如何在不超时的情况下使用mongoid/rails查询mongodb? [英] How can I query mongodb using mongoid/rails without timing out?
问题描述
我有一个rake任务,该任务处理一组记录并将其保存在 另一个集合:
I have a rake task that processes a set of records and saves it in another collection:
batch = []
Record.where(:type => 'a').each do |r|
batch << make_score(r)
if batch.size %100 == 0
Score.collection.insert(batch)
batch = []
end
end
我一次处理大约10万条记录.不幸的是,在20分钟时,出现Query response returned CURSOR_NOT_FOUND
错误.
I'm processing about 100K records at a time. Unfortunately at 20 minutes, I get a Query response returned CURSOR_NOT_FOUND
error.
mongodb faq 表示使用skip
和limit
或关闭超时,使用它们,所有操作的速度大约慢了2-3倍.
The mongodb faq says to use skip
and limit
or turn off timeouts, using them the all thing was about ~2-3 times slower.
如何关闭与蒙古包相关的超时?
How can I turn off timeouts in conjunction with mongoid?
推荐答案
MongoDB文档说您可以传入一个超时布尔值,并且超时为假,它将永远不会超时
The MongoDB docs say you can pass in a timeout boolean, and it timeout is false, it will never timeout
collection.find({"type" => "a"}, {:timeout=>false})
在您的情况下:
Record.collection.find({:type=>'a'}, :timeout => false).each ...
我还建议您研究使用Mongo简化的地图.似乎对这种集合数组操作进行了跟踪: http://www.mongodb.org/display /DOCS/MapReduce
I also recommend you look into map-reduced with Mongo. It seems tailer made to this sort of collection array manipulation: http://www.mongodb.org/display/DOCS/MapReduce
这篇关于如何在不超时的情况下使用mongoid/rails查询mongodb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!