From f50c6f3250b319f01476186261338ee5e04df00d Mon Sep 17 00:00:00 2001 From: Noah Stapp Date: Tue, 25 Mar 2025 10:43:31 -0400 Subject: [PATCH 1/2] PYTHON-4940 - Add index hint as an explicit parameter for distinct command --- pymongo/asynchronous/collection.py | 12 ++++++++++++ pymongo/synchronous/collection.py | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/pymongo/asynchronous/collection.py b/pymongo/asynchronous/collection.py index aef3539e8c..4a23b633ae 100644 --- a/pymongo/asynchronous/collection.py +++ b/pymongo/asynchronous/collection.py @@ -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 @@ -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. @@ -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], diff --git a/pymongo/synchronous/collection.py b/pymongo/synchronous/collection.py index fe869a622d..ea56fe989f 100644 --- a/pymongo/synchronous/collection.py +++ b/pymongo/synchronous/collection.py @@ -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 @@ -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. @@ -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], From 7c305eb08c6e39f9ed0358ebbfed841c3cc51b8d Mon Sep 17 00:00:00 2001 From: Noah Stapp Date: Tue, 25 Mar 2025 10:49:49 -0400 Subject: [PATCH 2/2] Update changelog --- doc/changelog.rst | 3 +++ pymongo/asynchronous/collection.py | 2 +- pymongo/synchronous/collection.py | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/doc/changelog.rst b/doc/changelog.rst index b172da6b8e..12991eeb29 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -9,6 +9,9 @@ PyMongo 4.12 brings a number of changes including: - Support for configuring DEK cache lifetime via the ``key_expiration_ms`` argument to :class:`~pymongo.encryption_options.AutoEncryptionOpts`. - Support for $lookup in CSFLE and QE supported on MongoDB 8.1+. +- Added index hinting support to the + :meth:`~pymongo.asynchronous.collection.AsyncCollection.distinct` and + :meth:`~pymongo.collection.Collection.distinct` commands. Issues Resolved ............... diff --git a/pymongo/asynchronous/collection.py b/pymongo/asynchronous/collection.py index 4a23b633ae..b87f207760 100644 --- a/pymongo/asynchronous/collection.py +++ b/pymongo/asynchronous/collection.py @@ -3169,7 +3169,7 @@ async def distinct( if hint is not None: if not isinstance(hint, str): hint = helpers_shared._index_document(hint) - cmd["hint"] = hint + cmd["hint"] = hint # type: ignore[assignment] async def _cmd( session: Optional[AsyncClientSession], diff --git a/pymongo/synchronous/collection.py b/pymongo/synchronous/collection.py index ea56fe989f..e63ed70fc2 100644 --- a/pymongo/synchronous/collection.py +++ b/pymongo/synchronous/collection.py @@ -3162,7 +3162,7 @@ def distinct( if hint is not None: if not isinstance(hint, str): hint = helpers_shared._index_document(hint) - cmd["hint"] = hint + cmd["hint"] = hint # type: ignore[assignment] def _cmd( session: Optional[ClientSession],