command_cursor – Tools for iterating over MongoDB command results

CommandCursor class to iterate over command results.

  • class pymongo.commandcursor.CommandCursor(_collection, cursor_info, address, retrieved=0, batch_size=0, max_await_time_ms=None, session=None, explicit_session=False)
  • Create a new command cursor.

The parameter ‘retrieved’ is unused.

  • address
  • The (host, port) of the server used, or None.

New in version 3.0.

  • alive
  • Does this cursor have the potential to return more data?

Even if alive is True, next() can raiseStopIteration. Best to use a for loop:

  1. for doc in collection.aggregate(pipeline):
  2. print(doc)

Note

alive can be True while iterating a cursor froma failed server. In this case alive will return False afternext() fails to retrieve the next batch of results from theserver.

  • batchsize(_batch_size)
  • Limits the number of documents returned in one batch. Each batchrequires a round trip to the server. It can be adjusted to optimizeperformance and limit data transfer.

Note

batch_size can not override MongoDB’s internal limits on theamount of data it will return to the client in a single batch (i.eif you set batch size to 1,000,000,000, MongoDB will currently onlyreturn 4-16MB of results per batch).

Raises TypeError if batch_size is not an integer.Raises ValueError if batch_size is less than 0.

Parameters:

  1. - _batch_size_: The size of each batch of results requested.
  • close()
  • Explicitly close / kill this cursor.

  • cursor_id

  • Returns the id of the cursor.

  • next()

  • Advance the cursor.

  • session

  • The cursor’s ClientSession, or None.

New in version 3.6.

  • class pymongo.commandcursor.RawBatchCommandCursor(_collection, cursor_info, address, retrieved=0, batch_size=0, max_await_time_ms=None, session=None, explicit_session=False)
  • Create a new cursor / iterator over raw batches of BSON data.

Should not be called directly by application developers -see aggregate_raw_batches()instead.

See also

The MongoDB documentation on

cursors