Execute a command or raise an error. :param dbname: name of the database on which to run the command :param spec: a command document as a dict, SON, or mapping object :param read_preference: a read preference :param codec_options: a CodecOptions instance :par
(
self,
dbname: str,
spec: MutableMapping[str, Any],
read_preference: _ServerMode = ReadPreference.PRIMARY,
codec_options: CodecOptions[Mapping[str, Any]] = DEFAULT_CODEC_OPTIONS, # type: ignore[assignment]
check: bool = True,
allowable_errors: Optional[Sequence[Union[str, int]]] = None,
read_concern: Optional[ReadConcern] = None,
write_concern: Optional[WriteConcern] = None,
parse_write_concern_error: bool = False,
collation: Optional[_CollationIn] = None,
session: Optional[ClientSession] = None,
client: Optional[MongoClient[Any]] = None,
retryable_write: bool = False,
publish_events: bool = True,
user_fields: Optional[Mapping[str, Any]] = None,
exhaust_allowed: bool = False,
)
| 342 | |
| 343 | @_handle_reauth |
| 344 | def command( |
| 345 | self, |
| 346 | dbname: str, |
| 347 | spec: MutableMapping[str, Any], |
| 348 | read_preference: _ServerMode = ReadPreference.PRIMARY, |
| 349 | codec_options: CodecOptions[Mapping[str, Any]] = DEFAULT_CODEC_OPTIONS, # type: ignore[assignment] |
| 350 | check: bool = True, |
| 351 | allowable_errors: Optional[Sequence[Union[str, int]]] = None, |
| 352 | read_concern: Optional[ReadConcern] = None, |
| 353 | write_concern: Optional[WriteConcern] = None, |
| 354 | parse_write_concern_error: bool = False, |
| 355 | collation: Optional[_CollationIn] = None, |
| 356 | session: Optional[ClientSession] = None, |
| 357 | client: Optional[MongoClient[Any]] = None, |
| 358 | retryable_write: bool = False, |
| 359 | publish_events: bool = True, |
| 360 | user_fields: Optional[Mapping[str, Any]] = None, |
| 361 | exhaust_allowed: bool = False, |
| 362 | ) -> dict[str, Any]: |
| 363 | """Execute a command or raise an error. |
| 364 | |
| 365 | :param dbname: name of the database on which to run the command |
| 366 | :param spec: a command document as a dict, SON, or mapping object |
| 367 | :param read_preference: a read preference |
| 368 | :param codec_options: a CodecOptions instance |
| 369 | :param check: raise OperationFailure if there are errors |
| 370 | :param allowable_errors: errors to ignore if `check` is True |
| 371 | :param read_concern: The read concern for this command. |
| 372 | :param write_concern: The write concern for this command. |
| 373 | :param parse_write_concern_error: Whether to parse the |
| 374 | ``writeConcernError`` field in the command response. |
| 375 | :param collation: The collation for this command. |
| 376 | :param session: optional ClientSession instance. |
| 377 | :param client: optional MongoClient for gossipping $clusterTime. |
| 378 | :param retryable_write: True if this command is a retryable write. |
| 379 | :param publish_events: Should we publish events for this command? |
| 380 | :param user_fields: Response fields that should be decoded |
| 381 | using the TypeDecoders from codec_options, passed to |
| 382 | bson._decode_all_selective. |
| 383 | """ |
| 384 | self.validate_session(client, session) |
| 385 | session = _validate_session_write_concern(session, write_concern) |
| 386 | |
| 387 | # Ensure command name remains in first place. |
| 388 | if not isinstance(spec, ORDERED_TYPES): # type:ignore[arg-type] |
| 389 | spec = dict(spec) |
| 390 | |
| 391 | if not (write_concern is None or write_concern.acknowledged or collation is None): |
| 392 | raise ConfigurationError("Collation is unsupported for unacknowledged writes.") |
| 393 | |
| 394 | self.add_server_api(spec) |
| 395 | if session: |
| 396 | session._apply_to(spec, retryable_write, read_preference, self) |
| 397 | self.send_cluster_time(spec, session, client) |
| 398 | listeners = self.listeners if publish_events else None |
| 399 | unacknowledged = bool(write_concern and not write_concern.acknowledged) |
| 400 | if self.op_msg_enabled: |
| 401 | self._raise_if_not_writable(unacknowledged) |