Method: Mongo::Cursor#next_document

Defined in:
lib/mongo/cursor.rb

#next_documentHash, Nil Also known as: next

Get the next document specified the cursor options.

Returns:

  • (Hash, Nil)

    the next document or Nil if no documents remain.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/mongo/cursor.rb', line 71

def next_document
  refresh if @cache.length == 0
  doc = @cache.shift

  if doc && doc['$err']
    err = doc['$err']

    # If the server has stopped being the master (e.g., it's one of a
    # pair but it has died or something like that) then we close that
    # connection. The next request will re-open on master server.
    if err == "not master"
      @connection.close
      raise ConnectionFailure, err
    end

    raise OperationFailure, err
  end

  doc
end