Skip to content

PYTHON-4677 Specify how maxTimeMS can be set for explain helpers #2439

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 5 commits into from
Jul 18, 2025
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
2 changes: 2 additions & 0 deletions pymongo/asynchronous/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,8 @@ async def explain(self) -> _DocumentType:
:meth:`~pymongo.asynchronous.database.AsyncDatabase.command` to run the explain
command directly.

.. note:: The timeout of this method can be set using :func:`pymongo.timeout`.

.. seealso:: The MongoDB documentation on `explain <https://dochub.mongodb.org/core/explain>`_.
"""
c = self.clone()
Expand Down
2 changes: 2 additions & 0 deletions pymongo/synchronous/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,8 @@ def explain(self) -> _DocumentType:
:meth:`~pymongo.database.Database.command` to run the explain
command directly.

.. note:: The timeout of this method can be set using :func:`pymongo.timeout`.

.. seealso:: The MongoDB documentation on `explain <https://dochub.mongodb.org/core/explain>`_.
"""
c = self.clone()
Expand Down
18 changes: 18 additions & 0 deletions test/asynchronous/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,24 @@ async def test_explain_with_read_concern(self):
self.assertEqual(len(started), 1)
self.assertNotIn("readConcern", started[0].command)

# https://github.com/mongodb/specifications/blob/master/source/crud/tests/README.md#14-explain-helpers-allow-users-to-specify-maxtimems
async def test_explain_csot(self):
# Create a MongoClient with command monitoring enabled (referred to as client).
listener = AllowListEventListener("explain")
client = await self.async_rs_or_single_client(event_listeners=[listener])

# Create a collection, referred to as collection, with the namespace explain-test.collection.
collection = client["explain-test"]["collection"]

# Run an explained find on collection. The find will have the query predicate { name: 'john doe' }. Specify a maxTimeMS value of 2000ms for the explain.
with pymongo.timeout(2.0):
self.assertTrue(await collection.find({"name": "john doe"}).explain())

# Obtain the command started event for the explain. Confirm that the top-level explain command should has a maxTimeMS value of 2000.
started = listener.started_events
self.assertEqual(len(started), 1)
assert 1500 < started[0].command["maxTimeMS"] <= 2000

async def test_hint(self):
db = self.db
with self.assertRaises(TypeError):
Expand Down
18 changes: 18 additions & 0 deletions test/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,24 @@ def test_explain_with_read_concern(self):
self.assertEqual(len(started), 1)
self.assertNotIn("readConcern", started[0].command)

# https://github.com/mongodb/specifications/blob/master/source/crud/tests/README.md#14-explain-helpers-allow-users-to-specify-maxtimems
def test_explain_csot(self):
# Create a MongoClient with command monitoring enabled (referred to as client).
listener = AllowListEventListener("explain")
client = self.rs_or_single_client(event_listeners=[listener])

# Create a collection, referred to as collection, with the namespace explain-test.collection.
collection = client["explain-test"]["collection"]

# Run an explained find on collection. The find will have the query predicate { name: 'john doe' }. Specify a maxTimeMS value of 2000ms for the explain.
with pymongo.timeout(2.0):
self.assertTrue(collection.find({"name": "john doe"}).explain())

# Obtain the command started event for the explain. Confirm that the top-level explain command should has a maxTimeMS value of 2000.
started = listener.started_events
self.assertEqual(len(started), 1)
assert 1500 < started[0].command["maxTimeMS"] <= 2000

def test_hint(self):
db = self.db
with self.assertRaises(TypeError):
Expand Down
Loading