我用下面的代碼來獲取蒙哥東西:寶石蒙戈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