@@ -20,25 +20,32 @@ Migrate to the {+driver-async+} Driver
20
20
Overview
21
21
--------
22
22
23
- The {+driver-async+} driver is a unification of {+driver-short+} and the `Motor
24
- library <https://www.mongodb.com/docs/drivers/motor/>`__. In this guide, you can
25
- identify the changes you must make to migrate an application from
26
- {+driver-short+} or Motor to the {+driver-async+} driver.
23
+ In September 2024, MongoDB released the {+driver-async+} driver to unify {+driver-short+}
24
+ and `Motor <https://www.mongodb.com/docs/drivers/motor/>`__, the asynchronous
25
+ MongoDB driver for Python. In this guide, you can identify the changes you must
26
+ make to migrate an application from {+driver-short+} or Motor to the
27
+ {+driver-async+} driver.
27
28
28
29
Migrate From {+driver-short+}
29
30
--------------------
30
31
31
32
The {+driver-async+} driver behaves similarly to {+driver-short+}, but
32
33
all methods that perform network operations are coroutines and must be awaited.
33
34
To migrate from {+driver-short+} to {+driver-async+}, you must update your code
34
- to replace all uses of ``MongoClient`` with ``AsyncMongoClient`` and add the
35
- ``await`` keyword to all asynchronous method calls. If an asynchronous method is
36
- called within a function, you must also mark the function as ``async``.
35
+ in the following ways:
36
+
37
+ - Replace all uses of ``MongoClient`` with ``AsyncMongoClient``.
38
+ - Add the ``await`` keyword to all asynchronous method calls .
39
+ - If an asynchronous method is called within a function, mark the function as ``async``.
40
+
41
+ The following sections describe how to implement the asynchronous API.
37
42
38
43
Asynchronous Methods
39
44
~~~~~~~~~~~~~~~~~~~~
40
45
41
- The following methods are asynchronous in {+driver-async+}:
46
+ The following tables list the asynchronous methods that are available in the
47
+ {+driver-async+} driver. To call these methods, you must ``await`` them and call them
48
+ inside an ``async`` function.
42
49
43
50
Client Methods
44
51
``````````````
@@ -318,8 +325,20 @@ Migrate From Motor
318
325
319
326
The {+driver-async+} driver behaves nearly identically to the Motor library. In
320
327
most cases, you can directly migrate existing Motor applications to
321
- {+driver-short+}, changing only the application's import statements, and
322
- by using ``AsyncMongoClient`` in place of ``MotorClient``.
328
+ {+driver-short+ by using ``AsyncMongoClient`` in place of ``MotorClient``, and
329
+ changing the application's import statements to import from ``pymongo``.
330
+
331
+ The following example shows the difference in imports to use a client for
332
+ read and write operations in Motor compared to {+driver-async+}:
333
+
334
+ .. code-block:: python
335
+
336
+ # Motor client import
337
+ from motor.motor_asyncio import AsyncIOMotorClient
338
+
339
+ # {+driver-async+} client import
340
+ from pymongo import AsyncMongoClient
341
+
323
342
324
343
The following section shows the method signature changes that you must make when
325
344
migrating from Motor to the {+driver-async+} driver:
@@ -336,4 +355,4 @@ The following Motor method signatures behave differently in the {+driver-async+}
336
355
- ``Cursor.each()`` does not exist in the {+driver-async+} driver.
337
356
- ``stream_to_handler()`` does not exist in the {+driver-async+} driver.
338
357
- ``to_list(0)`` is not valid in the {+driver-async+} driver. Use
339
- ``to_list(None)`` instead.
358
+ ``to_list(None)`` instead.
0 commit comments