鲁比·蒙戈司机的问题:
如何一次从集合输出5_000文档批次,直到读取集合中的最后一个文档,而不首先将整个数据库转储到内存中?
这对我来说真是个坏主意。
mongo = MongoClient.new('localhost', 27017)['sampledb']['samplecoll']
@whois.find.to_a....
发布于 2013-01-31 17:52:25
Mongo::Collection#find返回可枚举的Mongo::游标。对于批处理,Enumerable#each_slice是您的朋友,非常值得添加到您的工具包中。
希望你喜欢这个。
find_each_slice_test.rb
require 'mongo'
require 'test/unit'
class FindEachSliceTest < Test::Unit::TestCase
def setup
@samplecoll = Mongo::MongoClient.new('localhost', 27017)['sampledb']['samplecoll']
@samplecoll.remove
end
def test_find_each_slice
12345.times{|i| @samplecoll.insert( { i: i } ) }
slice__max_size = 5000
@samplecoll.find.each_slice(slice__max_size) do |slice|
puts "slice.size: #{slice.size}"
assert(slice__max_size >= slice.size)
end
end
end
红宝石find_each_slice_test.rb
Run options:
# Running tests:
slice.size: 5000
slice.size: 5000
slice.size: 2345
.
Finished tests in 6.979301s, 0.1433 tests/s, 0.4298 assertions/s.
1 tests, 3 assertions, 0 failures, 0 errors, 0 skips
https://stackoverflow.com/questions/14618647
复制相似问题