lib/mongo/cursor.rb
# Iterate through documents returned from the query.
#
# A cursor may be iterated at most once. Incomplete iteration is also
# allowed. Attempting to iterate the cursor more than once raises
# InvalidCursorOperation.
#
# @example Iterate over the documents in the cursor.
#   cursor.each do |doc|
#     ...
#   end
#
# @return [ Enumerator ] The enumerator.
#
# @since 2.0.0
Iterate through documents returned from the query.
A cursor may be iterated at most once. Incomplete iteration is also
allowed. Attempting to iterate the cursor more than once raises
InvalidCursorOperation.
Iterate through documents returned from the query . A cursor may be iterated at most once . Incomplete iteration is also allowed . Attempting to iterate the cursor more than once raises InvalidCursorOperation .
def each
       In a future driver version, each would either continue from the
       end of previous iteration or would always restart from the
       beginning.
      if @get_more_called
        raise Error::InvalidCursorOperation, 'Cannot restart iteration of a cursor which issued a getMore'
      end
       To maintain compatibility with pre-2.10 driver versions, reset
       the documents array each time a new iteration is started.
      @documents = nil
      if block_given?
         StopIteration raised by try_next ends this loop.
        loop do
          document = try_next
          yield document if document
        end
        self
      else
        documents = []
         StopIteration raised by try_next ends this loop.
        loop do
          document = try_next
          documents << document if document
        end
        documents
      end
    end
def each if @get_more_called raise Error :: InvalidCursorOperation , 'Cannot restart iteration of a cursor which issued a getMore' end @documents = nil if block_given? loop do document = try_next yield document if document end self else documents = [ ] loop do document = try_next documents << document if document end documents end end
Iterate through documents returned from the query.
Iterate through documents returned from the query .
# If we already iterated past the first batch (i.e., called get_more # at least once), the cursor on the server side has advanced past # the first batch and restarting iteration from the beginning by # returning initial result would miss documents in the second batch # and subsequent batches up to wherever the cursor is. Detect this # condition and abort the iteration. # # In a future driver version, each would either continue from the # end of previous iteration or would always restart from the # beginning. # To maintain compatibility with pre-2.10 driver versions, reset # the documents array each time a new iteration is started. # StopIteration raised by try_next ends this loop. # StopIteration raised by try_next ends this loop.
None
 Enumerator 
example
Iterate over the documents in the cursor. cursor.each do |doc| ... end
Iterate over the documents in the cursor . cursor . each do |doc| ... end
since
None