我用下面的代码来获取蒙哥东西:宝石蒙戈1.6.2返回错误值的计数(光标)
class BlockingMongoFetcher
include MongoConfig
def initialize
configure
@connection = Mongo::Connection.new(@server, @port)
@collection = init_collection(@connection)
end
def fetch(value)
mongo_cursor = @collection.find({ KEY => value.to_s })
if mongo_cursor.count == 0
# do stuff
return nil
end
if mongo_cursor.count > 1
# do stuff
end
mongo_cursor.first
end
end
init_collection只是得到数据库,并从连接的集合对象。 在获取方法中,我使用count方法来检查是否有结果。因为我有一个0,其中应该有1个项目,我添加宝石内下面的代码来Cursor类的计数方法:
if response['n'].to_i == 0
require "ruby-debug"
debugger
puts "stop here"
end
(响应= @ db.command(命令))
在调试器
@db.command(command)['n'].to_i
返回1。如果我的获取方法(一旦不使用输出)调用两次计一切都很好。我错过了什么吗?缓冲区还是缓存问题?但是,这似乎不是确定性的......它只发生在大约50%的运行中。 Mongodb是2.0.2和Ruby 1.9.3p125。
感谢您的帮助!
在你的问题,请您谈一下使用'count',但甚至没有出现在你的代码...请尝试构建一个最小的例子,其中的问题出现,使人们能够理解你的问题,找到解决办法。 – 2012-04-23 13:44:19