command_cursor – Tools for iterating over MongoDB command results

CommandCursor class to iterate over command results.

class pymongo.command_cursor.CommandCursor(collection: Collection[_DocumentType], cursor_info: Mapping[str, Any], address: Optional[Tuple[str, Optional[int]]], batch_size: int = 0, max_await_time_ms: Optional[int] = None, session: Optional[ClientSession] = None, explicit_session: bool = False, comment: Any = None)

Create a new command cursor.

property address: Optional[Tuple[str, Optional[int]]]

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

New in version 3.0.

property alive: bool

Does this cursor have the potential to return more data?

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

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

Note

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

batch_size(batch_size: int) pymongo.command_cursor.CommandCursor[pymongo.typings._DocumentType]

Limits the number of documents returned in one batch. Each batch requires a round trip to the server. It can be adjusted to optimize performance and limit data transfer.

Note

batch_size can not override MongoDB’s internal limits on the amount of data it will return to the client in a single batch (i.e if you set batch size to 1,000,000,000, MongoDB will currently only return 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
  • batch_size: The size of each batch of results requested.

close() None

Explicitly close / kill this cursor.

property cursor_id: int

Returns the id of the cursor.

next() pymongo.typings._DocumentType

Advance the cursor.

property session: Optional[ClientSession]

The cursor’s ClientSession, or None.

New in version 3.6.

class pymongo.command_cursor.RawBatchCommandCursor(collection: Collection[_DocumentType], cursor_info: Mapping[str, Any], address: Optional[Tuple[str, Optional[int]]], batch_size: int = 0, max_await_time_ms: Optional[int] = None, session: Optional[ClientSession] = None, explicit_session: bool = False, comment: Any = None)

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.