A cursor / iterator over command cursors.
| 44 | |
| 45 | |
| 46 | class CommandCursor(_CursorBase[_DocumentType]): |
| 47 | """A cursor / iterator over command cursors.""" |
| 48 | |
| 49 | _getmore_class = _GetMore |
| 50 | |
| 51 | def __init__( |
| 52 | self, |
| 53 | collection: Collection[_DocumentType], |
| 54 | cursor_info: Mapping[str, Any], |
| 55 | address: Optional[_Address], |
| 56 | batch_size: int = 0, |
| 57 | max_await_time_ms: Optional[int] = None, |
| 58 | session: Optional[ClientSession] = None, |
| 59 | comment: Any = None, |
| 60 | ) -> None: |
| 61 | """Create a new command cursor.""" |
| 62 | self._sock_mgr: Any = None |
| 63 | self._collection: Collection[_DocumentType] = collection |
| 64 | self._id = cursor_info["id"] |
| 65 | self._data = deque(cursor_info["firstBatch"]) |
| 66 | self._postbatchresumetoken: Optional[Mapping[str, Any]] = cursor_info.get( |
| 67 | "postBatchResumeToken" |
| 68 | ) |
| 69 | self._address = address |
| 70 | self._batch_size = batch_size |
| 71 | self._max_await_time_ms = max_await_time_ms |
| 72 | self._timeout = self._collection.database.client.options.timeout |
| 73 | self._session = session |
| 74 | if self._session is not None: |
| 75 | self._session._attached_to_cursor = True |
| 76 | self._killed = self._id == 0 |
| 77 | self._comment = comment |
| 78 | if self._killed: |
| 79 | self._end_session() |
| 80 | |
| 81 | if "ns" in cursor_info: # noqa: SIM401 |
| 82 | self._ns = cursor_info["ns"] |
| 83 | else: |
| 84 | self._ns = collection.full_name |
| 85 | |
| 86 | self.batch_size(batch_size) |
| 87 | |
| 88 | if not isinstance(max_await_time_ms, int) and max_await_time_ms is not None: |
| 89 | raise TypeError( |
| 90 | f"max_await_time_ms must be an integer or None, not {type(max_await_time_ms)}" |
| 91 | ) |
| 92 | |
| 93 | def _get_namespace(self) -> str: |
| 94 | return self._ns |
| 95 | |
| 96 | def batch_size(self, batch_size: int) -> CommandCursor[_DocumentType]: |
| 97 | """Limits the number of documents returned in one batch. Each batch |
| 98 | requires a round trip to the server. It can be adjusted to optimize |
| 99 | performance and limit data transfer. |
| 100 | |
| 101 | .. note:: batch_size can not override MongoDB's internal limits on the |
| 102 | amount of data it will return to the client in a single batch (i.e |
| 103 | if you set batch size to 1,000,000,000, MongoDB will currently only |
no outgoing calls