diff --git a/pymongo/asynchronous/collection.py b/pymongo/asynchronous/collection.py index 741c11e551..064231ccfc 100644 --- a/pymongo/asynchronous/collection.py +++ b/pymongo/asynchronous/collection.py @@ -2144,11 +2144,9 @@ async def count_documents( if comment is not None: kwargs["comment"] = comment pipeline.append({"$group": {"_id": 1, "n": {"$sum": 1}}}) - cmd = {"aggregate": self._name, "pipeline": pipeline, "cursor": {}} if "hint" in kwargs and not isinstance(kwargs["hint"], str): kwargs["hint"] = helpers_shared._index_document(kwargs["hint"]) collation = validate_collation_or_none(kwargs.pop("collation", None)) - cmd.update(kwargs) async def _cmd( session: Optional[AsyncClientSession], @@ -2156,6 +2154,8 @@ async def _cmd( conn: AsyncConnection, read_preference: Optional[_ServerMode], ) -> int: + cmd: dict[str, Any] = {"aggregate": self._name, "pipeline": pipeline, "cursor": {}} + cmd.update(kwargs) result = await self._aggregate_one_result( conn, read_preference, cmd, collation, session ) @@ -3194,19 +3194,14 @@ async def distinct( """ if not isinstance(key, str): raise TypeError(f"key must be an instance of str, not {type(key)}") - cmd = {"distinct": self._name, "key": key} if filter is not None: if "query" in kwargs: raise ConfigurationError("can't pass both filter and query") kwargs["query"] = filter collation = validate_collation_or_none(kwargs.pop("collation", None)) - cmd.update(kwargs) - if comment is not None: - cmd["comment"] = comment if hint is not None: if not isinstance(hint, str): hint = helpers_shared._index_document(hint) - cmd["hint"] = hint # type: ignore[assignment] async def _cmd( session: Optional[AsyncClientSession], @@ -3214,6 +3209,12 @@ async def _cmd( conn: AsyncConnection, read_preference: Optional[_ServerMode], ) -> list: # type: ignore[type-arg] + cmd = {"distinct": self._name, "key": key} + cmd.update(kwargs) + if comment is not None: + cmd["comment"] = comment + if hint is not None: + cmd["hint"] = hint # type: ignore[assignment] return ( await self._command( conn, @@ -3248,27 +3249,26 @@ async def _find_and_modify( f"return_document must be ReturnDocument.BEFORE or ReturnDocument.AFTER, not {type(return_document)}" ) collation = validate_collation_or_none(kwargs.pop("collation", None)) - cmd = {"findAndModify": self._name, "query": filter, "new": return_document} - if let is not None: - common.validate_is_mapping("let", let) - cmd["let"] = let - cmd.update(kwargs) - if projection is not None: - cmd["fields"] = helpers_shared._fields_list_to_dict(projection, "projection") - if sort is not None: - cmd["sort"] = helpers_shared._index_document(sort) - if upsert is not None: - validate_boolean("upsert", upsert) - cmd["upsert"] = upsert if hint is not None: if not isinstance(hint, str): hint = helpers_shared._index_document(hint) - - write_concern = self._write_concern_for_cmd(cmd, session) + write_concern = self._write_concern_for_cmd(kwargs, session) async def _find_and_modify_helper( session: Optional[AsyncClientSession], conn: AsyncConnection, retryable_write: bool ) -> Any: + cmd = {"findAndModify": self._name, "query": filter, "new": return_document} + if let is not None: + common.validate_is_mapping("let", let) + cmd["let"] = let + cmd.update(kwargs) + if projection is not None: + cmd["fields"] = helpers_shared._fields_list_to_dict(projection, "projection") + if sort is not None: + cmd["sort"] = helpers_shared._index_document(sort) + if upsert is not None: + validate_boolean("upsert", upsert) + cmd["upsert"] = upsert acknowledged = write_concern.acknowledged if array_filters is not None: if not acknowledged: diff --git a/pymongo/synchronous/collection.py b/pymongo/synchronous/collection.py index 9f32deb765..e5cc816cd3 100644 --- a/pymongo/synchronous/collection.py +++ b/pymongo/synchronous/collection.py @@ -2143,11 +2143,9 @@ def count_documents( if comment is not None: kwargs["comment"] = comment pipeline.append({"$group": {"_id": 1, "n": {"$sum": 1}}}) - cmd = {"aggregate": self._name, "pipeline": pipeline, "cursor": {}} if "hint" in kwargs and not isinstance(kwargs["hint"], str): kwargs["hint"] = helpers_shared._index_document(kwargs["hint"]) collation = validate_collation_or_none(kwargs.pop("collation", None)) - cmd.update(kwargs) def _cmd( session: Optional[ClientSession], @@ -2155,6 +2153,8 @@ def _cmd( conn: Connection, read_preference: Optional[_ServerMode], ) -> int: + cmd: dict[str, Any] = {"aggregate": self._name, "pipeline": pipeline, "cursor": {}} + cmd.update(kwargs) result = self._aggregate_one_result(conn, read_preference, cmd, collation, session) if not result: return 0 @@ -3187,19 +3187,14 @@ def distinct( """ if not isinstance(key, str): raise TypeError(f"key must be an instance of str, not {type(key)}") - cmd = {"distinct": self._name, "key": key} if filter is not None: if "query" in kwargs: raise ConfigurationError("can't pass both filter and query") kwargs["query"] = filter collation = validate_collation_or_none(kwargs.pop("collation", None)) - cmd.update(kwargs) - if comment is not None: - cmd["comment"] = comment if hint is not None: if not isinstance(hint, str): hint = helpers_shared._index_document(hint) - cmd["hint"] = hint # type: ignore[assignment] def _cmd( session: Optional[ClientSession], @@ -3207,6 +3202,12 @@ def _cmd( conn: Connection, read_preference: Optional[_ServerMode], ) -> list: # type: ignore[type-arg] + cmd = {"distinct": self._name, "key": key} + cmd.update(kwargs) + if comment is not None: + cmd["comment"] = comment + if hint is not None: + cmd["hint"] = hint # type: ignore[assignment] return ( self._command( conn, @@ -3241,27 +3242,26 @@ def _find_and_modify( f"return_document must be ReturnDocument.BEFORE or ReturnDocument.AFTER, not {type(return_document)}" ) collation = validate_collation_or_none(kwargs.pop("collation", None)) - cmd = {"findAndModify": self._name, "query": filter, "new": return_document} - if let is not None: - common.validate_is_mapping("let", let) - cmd["let"] = let - cmd.update(kwargs) - if projection is not None: - cmd["fields"] = helpers_shared._fields_list_to_dict(projection, "projection") - if sort is not None: - cmd["sort"] = helpers_shared._index_document(sort) - if upsert is not None: - validate_boolean("upsert", upsert) - cmd["upsert"] = upsert if hint is not None: if not isinstance(hint, str): hint = helpers_shared._index_document(hint) - - write_concern = self._write_concern_for_cmd(cmd, session) + write_concern = self._write_concern_for_cmd(kwargs, session) def _find_and_modify_helper( session: Optional[ClientSession], conn: Connection, retryable_write: bool ) -> Any: + cmd = {"findAndModify": self._name, "query": filter, "new": return_document} + if let is not None: + common.validate_is_mapping("let", let) + cmd["let"] = let + cmd.update(kwargs) + if projection is not None: + cmd["fields"] = helpers_shared._fields_list_to_dict(projection, "projection") + if sort is not None: + cmd["sort"] = helpers_shared._index_document(sort) + if upsert is not None: + validate_boolean("upsert", upsert) + cmd["upsert"] = upsert acknowledged = write_concern.acknowledged if array_filters is not None: if not acknowledged: