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: _Address | None, batch_size: int = 0, max_await_time_ms: int | None = None, session: ClientSession | None = None, explicit_session: bool = False, comment: Any = None)#
Create a new command cursor.
- property address: Tuple[str, int | None] | None#
The (host, port) of the server used, or None.
在 3.0 版本加入.
- property alive: bool#
Does this cursor have the potential to return more data?
Even if
aliveisTrue,next()can raiseStopIteration. Best to use a for loop:for doc in collection.aggregate(pipeline): print(doc)
- batch_size(batch_size: int) CommandCursor[_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.
备注
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
TypeErrorif batch_size is not an integer. RaisesValueErrorif batch_size is less than0.- Parameters:
batch_size: The size of each batch of results requested.
- next() _DocumentType#
Advance the cursor.
- property session: ClientSession | None#
The cursor's
ClientSession, or None.在 3.6 版本加入.
- try_next() _DocumentType | None#
Advance the cursor without blocking indefinitely.
This method returns the next document without waiting indefinitely for data.
If no document is cached locally then this method runs a single getMore command. If the getMore yields any documents, the next document is returned, otherwise, if the getMore returns no documents (because there is no additional data) then
Noneis returned.- Returns:
The next document or
Nonewhen no document is available after running a single getMore or when the cursor is closed.
在 4.5 版本加入.
- class pymongo.command_cursor.RawBatchCommandCursor(collection: Collection[_DocumentType], cursor_info: Mapping[str, Any], address: _Address | None, batch_size: int = 0, max_await_time_ms: int | None = None, session: ClientSession | None = 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.参见
The MongoDB documentation on cursors.