lib/mongo/cursor.rb
def try_next
      if @documents.nil?
        # Since published versions of Mongoid have a copy of old driver cursor
        # code, our dup call in #process isn't invoked when Mongoid query
        # cache is active. Work around that by also calling dup here on
        # the result of #process which might come out of Mongoid's code.
        @documents = process(@initial_result).dup
        # the documents here can be an empty array, hence
        # we may end up issuing a getMore in the first try_next call
      end

      if @documents.empty?
        # On empty batches, we cache the batch resume token
        cache_batch_resume_token

        unless closed?
          if exhausted?
            close
            @fully_iterated = true
            raise StopIteration
          end
          @documents = get_more
        else
          @fully_iterated = true
          raise StopIteration
        end
      else
        # cursor is closed here
        # keep documents as an empty array
      end

      # If there is at least one document, cache its _id
      if @documents[0]
        cache_resume_token(@documents[0])
      end

      # Cache the batch resume token if we are iterating
      # over the last document, or if the batch is empty
      if @documents.size <= 1
        cache_batch_resume_token
        if closed?
          @fully_iterated = true
        end
      end

      return @documents.shift
    end
# Return one document from the query, if one is available.
#
# This method will wait up to max_await_time_ms milliseconds
# for changes from the server, and if no changes are received
# it will return nil. If there are no more documents to return
# from the server, or if we have exhausted the cursor, it will
# raise a StopIteration exception.
#
# @note This method is experimental and subject to change.
#
# @return [ BSON::Document | nil ] A document.
#
# @raise [ StopIteration ] Raised on the calls after the cursor had been
#   completely iterated.
#
# @api private
Return one document from the query, if one is available.
This method will wait up to max_await_time_ms milliseconds
for changes from the server, and if no changes are received
it will return nil. If there are no more documents to return
from the server, or if we have exhausted the cursor, it will
raise a StopIteration exception.
Return one document from the query if one is available . This method will wait up to max_await_time_ms milliseconds for changes from the server and if no changes are received it will return nil . If there are no more documents to return from the server or if we have exhausted the cursor it will raise a StopIteration exception .
def try_next
      if @documents.nil?
        @documents = process(@initial_result).dup
      end
      if @documents.empty?
        cache_batch_resume_token
        unless closed?
          if exhausted?
            close
            @fully_iterated = true
            raise StopIteration
          end
          @documents = get_more
        else
          @fully_iterated = true
          raise StopIteration
        end
      else
      end
      if @documents[0]
        cache_resume_token(@documents[0])
      end
      if @documents.size <= 1
        cache_batch_resume_token
        if closed?
          @fully_iterated = true
        end
      end
      return @documents.shift
    end
def try_next if @documents . nil? @documents = process ( @initial_result ) . dup end if @documents . empty? cache_batch_resume_token unless closed? if exhausted? close @fully_iterated = true raise StopIteration end @documents = get_more else @fully_iterated = true raise StopIteration end else end if @documents [ 0 ] cache_resume_token ( @documents [ 0 ] ) end if @documents . size <= 1 cache_batch_resume_token if closed? @fully_iterated = true end end return @documents . shift end
Return one document from the query, if one is available.
Return one document from the query if one is available .
# Since published versions of Mongoid have a copy of old driver cursor # code, our dup call in #process isn't invoked when Mongoid query # cache is active. Work around that by also calling dup here on # the result of #process which might come out of Mongoid's code. # the documents here can be an empty array, hence # we may end up issuing a getMore in the first try_next call # On empty batches, we cache the batch resume token # cursor is closed here # keep documents as an empty array # If there is at least one document, cache its _id # Cache the batch resume token if we are iterating # over the last document, or if the batch is empty
None
 BSON::Document | nil 
[ StopIteration ] Raised on the calls after the cursor had been completely iterated.
[ StopIteration ] Raised on the calls after the cursor had been completely iterated .
note
This method is experimental and subject to change.
This method is experimental and subject to change .
api
None