Skip to content

Commit bf4c24d

Browse files
committed
PYTHON-3606 - Document best practice for closing MongoClients and cursors
1 parent bbb6f88 commit bf4c24d

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

pymongo/asynchronous/collection.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,6 +1776,13 @@ def find(self, *args: Any, **kwargs: Any) -> AsyncCursor[_DocumentType]:
17761776
improper type. Returns an instance of
17771777
:class:`~pymongo.asynchronous.cursor.AsyncCursor` corresponding to this query.
17781778
1779+
Best practice is to call :meth:`AsyncCursor.close` when the cursor is no longer needed,
1780+
or use the cursor in a with statement::
1781+
1782+
async with collection.find() as cursor:
1783+
async for doc in cursor:
1784+
print(doc)
1785+
17791786
The :meth:`find` method obeys the :attr:`read_preference` of
17801787
this :class:`AsyncCollection`.
17811788

pymongo/asynchronous/mongo_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,12 @@ def __init__(
202202
exception (recognizing that the operation failed) and then continue to
203203
execute.
204204
205+
Best practice is to call :meth:`AsyncMongoClient.close` when the client is no longer needed,
206+
or use the client in a with statement::
207+
208+
with AsyncMongoClient(url) as client:
209+
# Use client here.
210+
205211
The `host` parameter can be a full `mongodb URI
206212
<https://dochub.mongodb.org/core/connections>`_, in addition to
207213
a simple hostname. It can also be a list of hostnames but no more

pymongo/synchronous/collection.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,6 +1775,13 @@ def find(self, *args: Any, **kwargs: Any) -> Cursor[_DocumentType]:
17751775
improper type. Returns an instance of
17761776
:class:`~pymongo.cursor.Cursor` corresponding to this query.
17771777
1778+
Best practice is to call :meth:`Cursor.close` when the cursor is no longer needed,
1779+
or use the cursor in a with statement::
1780+
1781+
with collection.find() as cursor:
1782+
for doc in cursor:
1783+
print(doc)
1784+
17781785
The :meth:`find` method obeys the :attr:`read_preference` of
17791786
this :class:`Collection`.
17801787

pymongo/synchronous/mongo_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ def __init__(
199199
exception (recognizing that the operation failed) and then continue to
200200
execute.
201201
202+
Best practice is to call :meth:`MongoClient.close` when the client is no longer needed,
203+
or use the client in a with statement::
204+
205+
with MongoClient(url) as client:
206+
# Use client here.
207+
202208
The `host` parameter can be a full `mongodb URI
203209
<https://dochub.mongodb.org/core/connections>`_, in addition to
204210
a simple hostname. It can also be a list of hostnames but no more

0 commit comments

Comments
 (0)