Skip to content

Commit 4403169

Browse files
authored
PYTHON-4940 - Add index hint as an explicit parameter for distinct command. (mongodb#2225)
1 parent fa5e637 commit 4403169

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

doc/changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ PyMongo 4.12 brings a number of changes including:
99
- Support for configuring DEK cache lifetime via the ``key_expiration_ms`` argument to
1010
:class:`~pymongo.encryption_options.AutoEncryptionOpts`.
1111
- Support for $lookup in CSFLE and QE supported on MongoDB 8.1+.
12+
- Added index hinting support to the
13+
:meth:`~pymongo.asynchronous.collection.AsyncCollection.distinct` and
14+
:meth:`~pymongo.collection.Collection.distinct` commands.
1215

1316
Issues Resolved
1417
...............

pymongo/asynchronous/collection.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3111,6 +3111,7 @@ async def distinct(
31113111
filter: Optional[Mapping[str, Any]] = None,
31123112
session: Optional[AsyncClientSession] = None,
31133113
comment: Optional[Any] = None,
3114+
hint: Optional[_IndexKeyHint] = None,
31143115
**kwargs: Any,
31153116
) -> list:
31163117
"""Get a list of distinct values for `key` among all documents
@@ -3138,8 +3139,15 @@ async def distinct(
31383139
:class:`~pymongo.asynchronous.client_session.AsyncClientSession`.
31393140
:param comment: A user-provided comment to attach to this
31403141
command.
3142+
:param hint: An index to use to support the query
3143+
predicate specified either by its string name, or in the same
3144+
format as passed to :meth:`~pymongo.asynchronous.collection.AsyncCollection.create_index`
3145+
(e.g. ``[('field', ASCENDING)]``).
31413146
:param kwargs: See list of options above.
31423147
3148+
.. versionchanged:: 4.12
3149+
Added ``hint`` parameter.
3150+
31433151
.. versionchanged:: 3.6
31443152
Added ``session`` parameter.
31453153
@@ -3158,6 +3166,10 @@ async def distinct(
31583166
cmd.update(kwargs)
31593167
if comment is not None:
31603168
cmd["comment"] = comment
3169+
if hint is not None:
3170+
if not isinstance(hint, str):
3171+
hint = helpers_shared._index_document(hint)
3172+
cmd["hint"] = hint # type: ignore[assignment]
31613173

31623174
async def _cmd(
31633175
session: Optional[AsyncClientSession],

pymongo/synchronous/collection.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3104,6 +3104,7 @@ def distinct(
31043104
filter: Optional[Mapping[str, Any]] = None,
31053105
session: Optional[ClientSession] = None,
31063106
comment: Optional[Any] = None,
3107+
hint: Optional[_IndexKeyHint] = None,
31073108
**kwargs: Any,
31083109
) -> list:
31093110
"""Get a list of distinct values for `key` among all documents
@@ -3131,8 +3132,15 @@ def distinct(
31313132
:class:`~pymongo.client_session.ClientSession`.
31323133
:param comment: A user-provided comment to attach to this
31333134
command.
3135+
:param hint: An index to use to support the query
3136+
predicate specified either by its string name, or in the same
3137+
format as passed to :meth:`~pymongo.collection.Collection.create_index`
3138+
(e.g. ``[('field', ASCENDING)]``).
31343139
:param kwargs: See list of options above.
31353140
3141+
.. versionchanged:: 4.12
3142+
Added ``hint`` parameter.
3143+
31363144
.. versionchanged:: 3.6
31373145
Added ``session`` parameter.
31383146
@@ -3151,6 +3159,10 @@ def distinct(
31513159
cmd.update(kwargs)
31523160
if comment is not None:
31533161
cmd["comment"] = comment
3162+
if hint is not None:
3163+
if not isinstance(hint, str):
3164+
hint = helpers_shared._index_document(hint)
3165+
cmd["hint"] = hint # type: ignore[assignment]
31543166

31553167
def _cmd(
31563168
session: Optional[ClientSession],

0 commit comments

Comments
 (0)