Ruby Mongo驱动程序问题:
如何从集合中一次输出5_000个文档批次,直到我读取集合中的最后一个文档而不先将整个数据库转储到内存中?
对我来说这是非常糟糕的方法:
mongo = MongoClient.new('localhost', 27017)['sampledb']['samplecoll']
@whois.find.to_a....
答案 0 :(得分:1)
Mongo :: Collection#find返回一个可枚举的Mongo :: Cursor。对于批处理,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
ruby 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