Method: Mongo::Cursor#each

Defined in:
lib/mongo/cursor.rb

#each { ... } ⇒ Object

Iterate over each document in this cursor, yielding it to the given block.

Iterating over an entire cursor will close it.

Examples:

if ‘comments’ represents a collection of comments:

comments.find.each do |doc|
  puts doc['user']
end

Yields:

  • passes each document to a block for processing.



226
227
228
229
230
231
232
233
# File 'lib/mongo/cursor.rb', line 226

def each
  #num_returned = 0
  #while has_next? && (@limit <= 0 || num_returned < @limit)
  while doc = next_document
    yield doc #next_document
    #num_returned += 1
  end
end