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

This commit is contained in:
Noah Stapp 2025-03-25 11:20:10 -04:00 committed by GitHub
parent fa5e637da8
commit 4403169821
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 0 deletions

View File

@ -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
...............

View File

@ -3111,6 +3111,7 @@ class AsyncCollection(common.BaseObject, Generic[_DocumentType]):
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 @@ class AsyncCollection(common.BaseObject, Generic[_DocumentType]):
: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 @@ class AsyncCollection(common.BaseObject, Generic[_DocumentType]):
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],

View File

@ -3104,6 +3104,7 @@ class Collection(common.BaseObject, Generic[_DocumentType]):
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 @@ class Collection(common.BaseObject, Generic[_DocumentType]):
: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 @@ class Collection(common.BaseObject, Generic[_DocumentType]):
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],