Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions pymongo/asynchronous/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2144,18 +2144,18 @@ 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],
_server: Server,
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
)
Expand Down Expand Up @@ -3194,26 +3194,27 @@ 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],
_server: Server,
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,
Expand Down Expand Up @@ -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:
Expand Down
42 changes: 21 additions & 21 deletions pymongo/synchronous/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2143,18 +2143,18 @@ 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],
_server: Server,
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
Expand Down Expand Up @@ -3187,26 +3187,27 @@ 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],
_server: Server,
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,
Expand Down Expand Up @@ -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:
Expand Down
Loading