-
Notifications
You must be signed in to change notification settings - Fork 20
DOCSP-41887 Async Migration Guide #103
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
Changes from 4 commits
c29250e
7a49198
5e2d5a5
ca264a9
e36034b
bc1a8a3
3e9acc3
edfff73
3045f0d
6f728ce
301ba14
6637b87
102c829
afc9ddf
bf38a06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,339 @@ | ||
.. _pymongo-async-migration: | ||
|
||
=================================== | ||
Migrate to the {+driver-async+} Driver | ||
=================================== | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: motor, async, refactor, migration | ||
|
||
Overview | ||
-------- | ||
|
||
The {+driver-async+} driver is a unification of {+driver-short+} and the `Motor | ||
library <https://www.mongodb.com/docs/drivers/motor/>`__. In this guide, you can | ||
jordan-smith721 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
identify the changes you must make to migrate an application from | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, the word "migrate" seems a little confusing for existing PyMongo users. Is there a different wording we can use to make sure users understand that they only need to make changes if they want to use the new async API? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reworded this after splitting pages |
||
{+driver-short+} or Motor to the {+driver-async+} driver. | ||
|
||
Migrate From {+driver-short+} | ||
-------------------- | ||
|
||
The {+driver-async+} driver behaves similarly to {+driver-short+}, but | ||
all methods that perform network operations are coroutines and must be awaited. | ||
To migrate from {+driver-short+} to {+driver-async+}, you must update your code | ||
to replace all uses of ``MongoClient`` with ``AsyncMongoClient`` and add the | ||
``await`` keyword to all asynchronous method calls. If an asynchronous method is | ||
called within a function, you must also mark the function as ``async``. | ||
jordan-smith721 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
rustagir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Asynchronous Methods | ||
~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The following methods are asynchronous in {+driver-async+}: | ||
jordan-smith721 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Client Methods | ||
`````````````` | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
:widths: 20 80 | ||
|
||
* - Method | ||
- Example | ||
|
||
* - ``AsyncMongoClient()`` | ||
- .. code-block:: python | ||
|
||
from pymongo import AsyncMongoClient | ||
|
||
client = AsyncMongoClient(...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S: perhaps this should have its own subsection as its a constructor rather than a method? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about that at first, but it seemed weird to be in its own table and it's related to the client so I figured it made sense here. Can change if anyone has strong feelings the other way though |
||
|
||
* - ``watch()`` | ||
- .. code-block:: python | ||
|
||
async with await client.watch(...) as stream: | ||
... | ||
jordan-smith721 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
* - ``server_info()`` | ||
- .. code-block:: python | ||
|
||
info = await client.server_info(...) | ||
jordan-smith721 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
* - ``list_databases()`` | ||
- .. code-block:: python | ||
|
||
databases = await client.list_databases() | ||
|
||
* - ``list_database_names()`` | ||
- .. code-block:: python | ||
|
||
database_names = await client.list_database_names() | ||
|
||
* - ``drop_database()`` | ||
- .. code-block:: python | ||
|
||
await client.drop_database(...) | ||
|
||
Database Methods | ||
```````````````` | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
:widths: 20 80 | ||
|
||
* - Method | ||
- Example | ||
|
||
* - ``watch()`` | ||
- .. code-block:: python | ||
|
||
async with await db.watch(...) as stream: | ||
... | ||
jordan-smith721 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
* - ``create_collection()`` | ||
- .. code-block:: python | ||
|
||
await db.create_collection(...) | ||
|
||
* - ``aggregate()`` | ||
- .. code-block:: python | ||
|
||
async with await client.admin.aggregate(...) as cursor: | ||
... | ||
jordan-smith721 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
* - ``command()`` | ||
- .. code-block:: python | ||
|
||
result = await db.command(...) | ||
|
||
* - ``cursor_command()`` | ||
- .. code-block:: python | ||
|
||
curr = await db.cursor_command(...) | ||
|
||
* - ``list_collections()`` | ||
- .. code-block:: python | ||
|
||
collections = await db.list_collections() | ||
|
||
* - ``list_collection_names()`` | ||
- .. code-block:: python | ||
|
||
collection_names = await db.list_collection_names() | ||
|
||
* - ``drop_collection()`` | ||
- .. code-block:: python | ||
|
||
await db.drop_collection(...) | ||
|
||
* - ``validate_collection()`` | ||
- .. code-block:: python | ||
|
||
result = await db.validate_collection(...) | ||
|
||
* - ``dereference()`` | ||
- .. code-block:: python | ||
|
||
result = await db.dereference(...) | ||
|
||
Collection Methods | ||
`````````````````` | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
:widths: 20 80 | ||
|
||
* - Method | ||
- Example | ||
|
||
* - ``watch()`` | ||
- .. code-block:: python | ||
|
||
async with await collection.watch(...) as stream: | ||
... | ||
|
||
* - ``insert_one()`` | ||
- .. code-block:: python | ||
|
||
await collection.insert_one(...) | ||
|
||
* - ``insert_many()`` | ||
- .. code-block:: python | ||
|
||
await collection.insert_many(...) | ||
|
||
* - ``replace_one()`` | ||
- .. code-block:: python | ||
|
||
await collection.replace_one(...) | ||
|
||
* - ``update_one()`` | ||
- .. code-block:: python | ||
|
||
await collection.update_one(...) | ||
|
||
* - ``update_many()`` | ||
- .. code-block:: python | ||
|
||
await collection.update_many(...) | ||
|
||
* - ``drop()`` | ||
- .. code-block:: python | ||
|
||
await collection.drop() | ||
|
||
* - ``delete_one()`` | ||
- .. code-block:: python | ||
|
||
await collection.delete_one(...) | ||
|
||
* - ``delete_many()`` | ||
- .. code-block:: python | ||
|
||
await collection.delete_many(...) | ||
|
||
* - ``find_one()`` | ||
- .. code-block:: python | ||
|
||
result = await collection.find_one(...) | ||
|
||
* - ``estimated_document_count()`` | ||
- .. code-block:: python | ||
|
||
count = await collection.estimated_document_count() | ||
|
||
* - ``count_documents()`` | ||
- .. code-block:: python | ||
|
||
count = await collection.count_documents(...) | ||
|
||
* - ``create_index()`` | ||
- .. code-block:: python | ||
|
||
await collection.create_index(...) | ||
|
||
* - ``create_indexes()`` | ||
- .. code-block:: python | ||
|
||
await collection.create_indexes(...) | ||
|
||
* - ``drop_index()`` | ||
- .. code-block:: python | ||
|
||
await collection.drop_index(...) | ||
|
||
* - ``drop_indexes()`` | ||
- .. code-block:: python | ||
|
||
await collection.drop_indexes() | ||
|
||
* - ``list_indexes()`` | ||
- .. code-block:: python | ||
|
||
indexes = await collection.list_indexes() | ||
|
||
* - ``index_information()`` | ||
- .. code-block:: python | ||
|
||
info = await collection.index_information() | ||
|
||
* - ``list_search_indexes()`` | ||
- .. code-block:: python | ||
|
||
indexes = await collection.list_search_indexes() | ||
|
||
* - ``create_search_index()`` | ||
- .. code-block:: python | ||
|
||
await collection.create_search_index(...) | ||
|
||
* - ``create_search_indexes()`` | ||
- .. code-block:: python | ||
|
||
await collection.create_search_indexes(...) | ||
|
||
* - ``drop_search_index()`` | ||
- .. code-block:: python | ||
|
||
await collection.drop_search_index(...) | ||
|
||
* - ``update_search_index()`` | ||
- .. code-block:: python | ||
|
||
await collection.update_search_index(...) | ||
|
||
* - ``options()`` | ||
- .. code-block:: python | ||
|
||
options = await collection.options() | ||
|
||
* - ``aggregate()`` | ||
- .. code-block:: python | ||
|
||
async for doc in await collection.aggregate(...): | ||
... | ||
|
||
* - ``aggregate_raw_batches()`` | ||
- .. code-block:: python | ||
|
||
async for batch in await collection.aggregate_raw_batches(...): | ||
... | ||
|
||
* - ``rename()`` | ||
- .. code-block:: python | ||
|
||
await collection.rename(...) | ||
|
||
* - ``distinct()`` | ||
- .. code-block:: python | ||
|
||
distinct_values = await collection.distinct(...) | ||
|
||
* - ``find_one_and_delete()`` | ||
- .. code-block:: python | ||
|
||
result = await collection.find_one_and_delete(...) | ||
|
||
* - ``find_one_and_replace()`` | ||
- .. code-block:: python | ||
|
||
result = await collection.find_one_and_replace(...) | ||
|
||
* - ``find_one_and_update()`` | ||
- .. code-block:: python | ||
|
||
result = await collection.find_one_and_update(...) | ||
|
||
Migrate From Motor | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to see the motor -> async pymongo migration as its own dedicated page. The pymongo sync -> async differences clutters the page for motor users. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Split the pages into two |
||
------------------ | ||
|
||
The {+driver-async+} driver behaves nearly identically to the Motor library. In | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a little misleading. Async PyMongo is (or should be) vastly superior to Motor in behavior both in terms of latency and throughput because Async Pymongo directly uses asyncio whereas Motor delegates all work to a thread pool. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated the wording here, let me know if that works better! |
||
most cases, you can directly migrate existing Motor applications to | ||
{+driver-short+}, changing only the application's import statements, and | ||
by using ``AsyncMongoClient`` in place of ``MotorClient``. | ||
|
||
The following section shows the method signature changes that you must make when | ||
migrating from Motor to the {+driver-async+} driver: | ||
jordan-smith721 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Method Signature Changes | ||
~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The following Motor method signatures behave differently in the {+driver-async+} driver: | ||
|
||
- ``GridOut.open()`` returns ``None``. | ||
- ``AsyncMongoClient.__init__()`` does not accept an ``io_loop`` parameter. | ||
- ``GridIn.set()`` does not accept a filename. Instead pass a file name by using the | ||
``GridIn.filename`` attribute. | ||
- ``Cursor.each()`` does not exist in the {+driver-async+} driver. | ||
- ``stream_to_handler()`` does not exist in the {+driver-async+} driver. | ||
- ``to_list(0)`` is not valid in the {+driver-async+} driver. Use | ||
``to_list(None)`` instead. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
S: I think another sentence here could be useful before linking to the guide. Maybe you can mention the time period that this applies to?
Eg.
In Aug 2024, MongoDB released the PyMongo Async driver to unify PyMongo and Motor, the asynchronous MongoDB driver for Python.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flagging this again, but also ok if you wanted to leave as is!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, misread this the first time and applied it to the wrong area. Fixed!