Skip to content

PYTHON-4940 - Add index hint as an explicit parameter for distinct command. #2225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions pymongo/asynchronous/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3111,6 +3111,7 @@ async def distinct(
filter: Optional[Mapping[str, Any]] = None,
session: Optional[AsyncClientSession] = None,
comment: Optional[Any] = None,
hint: Optional[_IndexKeyHint] = None,
**kwargs: Any,
) -> list:
"""Get a list of distinct values for `key` among all documents
Expand Down Expand Up @@ -3138,8 +3139,15 @@ async def distinct(
:class:`~pymongo.asynchronous.client_session.AsyncClientSession`.
:param comment: A user-provided comment to attach to this
command.
:param hint: An index to use to support the query
predicate specified either by its string name, or in the same
format as passed to :meth:`~pymongo.asynchronous.collection.AsyncCollection.create_index`
(e.g. ``[('field', ASCENDING)]``).
:param kwargs: See list of options above.

.. versionchanged:: 4.12
Added ``hint`` parameter.

.. versionchanged:: 3.6
Added ``session`` parameter.

Expand All @@ -3158,6 +3166,10 @@ async def distinct(
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

async def _cmd(
session: Optional[AsyncClientSession],
Expand Down
12 changes: 12 additions & 0 deletions pymongo/synchronous/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3104,6 +3104,7 @@ def distinct(
filter: Optional[Mapping[str, Any]] = None,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
hint: Optional[_IndexKeyHint] = None,
**kwargs: Any,
) -> list:
"""Get a list of distinct values for `key` among all documents
Expand Down Expand Up @@ -3131,8 +3132,15 @@ def distinct(
:class:`~pymongo.client_session.ClientSession`.
:param comment: A user-provided comment to attach to this
command.
:param hint: An index to use to support the query
predicate specified either by its string name, or in the same
format as passed to :meth:`~pymongo.collection.Collection.create_index`
(e.g. ``[('field', ASCENDING)]``).
:param kwargs: See list of options above.

.. versionchanged:: 4.12
Added ``hint`` parameter.

.. versionchanged:: 3.6
Added ``session`` parameter.

Expand All @@ -3151,6 +3159,10 @@ def distinct(
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

def _cmd(
session: Optional[ClientSession],
Expand Down
Loading