Module: MongoModel::Scope::Batches

Included in:
MongoModel::Scope
Defined in:
lib/mongomodel/support/scope/batches.rb

Instance Method Summary collapse

Instance Method Details

#in_batches(batch_size = 1000) ⇒ Object


4
5
6
7
8
9
10
11
12
# File 'lib/mongomodel/support/scope/batches.rb', line 4

def in_batches(batch_size=1000)
  offset = 0

  begin
    documents = offset(offset).limit(batch_size).all
    yield documents if block_given? && !documents.empty?
    offset += batch_size
  end until documents.size < batch_size
end