@@ -20,11 +20,10 @@ Migrate to the {+driver-async+} Driver
20
20
Overview
21
21
--------
22
22
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.
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.
28
27
29
28
Migrate From {+driver-short+}
30
29
--------------------
@@ -35,7 +34,7 @@ To migrate from {+driver-short+} to {+driver-async+}, you must update your code
35
34
in the following ways:
36
35
37
36
- Replace all uses of ``MongoClient`` with ``AsyncMongoClient``.
38
- - Add the ``await`` keyword to all asynchronous method calls .
37
+ - Add the ``await`` keyword to all asynchronous method calls.
39
38
- If an asynchronous method is called within a function, mark the function as ``async``.
40
39
41
40
The following sections describe how to implement the asynchronous API.
@@ -62,28 +61,28 @@ Client Methods
62
61
63
62
from pymongo import AsyncMongoClient
64
63
65
- client = AsyncMongoClient(...)
64
+ AsyncMongoClient(...)
66
65
67
66
* - ``watch()``
68
67
- .. code-block:: python
69
68
70
69
async with await client.watch(...) as stream:
71
- ...
70
+ ...
72
71
73
72
* - ``server_info()``
74
73
- .. code-block:: python
75
74
76
- info = await client.server_info(...)
75
+ await client.server_info(...)
77
76
78
77
* - ``list_databases()``
79
78
- .. code-block:: python
80
79
81
- databases = await client.list_databases()
80
+ await client.list_databases()
82
81
83
82
* - ``list_database_names()``
84
83
- .. code-block:: python
85
84
86
- database_names = await client.list_database_names()
85
+ await client.list_database_names()
87
86
88
87
* - ``drop_database()``
89
88
- .. code-block:: python
@@ -104,7 +103,7 @@ Database Methods
104
103
- .. code-block:: python
105
104
106
105
async with await db.watch(...) as stream:
107
- ...
106
+ ...
108
107
109
108
* - ``create_collection()``
110
109
- .. code-block:: python
@@ -115,27 +114,27 @@ Database Methods
115
114
- .. code-block:: python
116
115
117
116
async with await client.admin.aggregate(...) as cursor:
118
- ...
117
+ ...
119
118
120
119
* - ``command()``
121
120
- .. code-block:: python
122
121
123
- result = await db.command(...)
122
+ await db.command(...)
124
123
125
124
* - ``cursor_command()``
126
125
- .. code-block:: python
127
126
128
- curr = await db.cursor_command(...)
127
+ await db.cursor_command(...)
129
128
130
129
* - ``list_collections()``
131
130
- .. code-block:: python
132
131
133
- collections = await db.list_collections()
132
+ await db.list_collections()
134
133
135
134
* - ``list_collection_names()``
136
135
- .. code-block:: python
137
136
138
- collection_names = await db.list_collection_names()
137
+ await db.list_collection_names()
139
138
140
139
* - ``drop_collection()``
141
140
- .. code-block:: python
@@ -145,12 +144,12 @@ Database Methods
145
144
* - ``validate_collection()``
146
145
- .. code-block:: python
147
146
148
- result = await db.validate_collection(...)
147
+ await db.validate_collection(...)
149
148
150
149
* - ``dereference()``
151
150
- .. code-block:: python
152
151
153
- result = await db.dereference(...)
152
+ await db.dereference(...)
154
153
155
154
Collection Methods
156
155
``````````````````
@@ -166,7 +165,7 @@ Collection Methods
166
165
- .. code-block:: python
167
166
168
167
async with await collection.watch(...) as stream:
169
- ...
168
+ ...
170
169
171
170
* - ``insert_one()``
172
171
- .. code-block:: python
@@ -211,17 +210,17 @@ Collection Methods
211
210
* - ``find_one()``
212
211
- .. code-block:: python
213
212
214
- result = await collection.find_one(...)
213
+ await collection.find_one(...)
215
214
216
215
* - ``estimated_document_count()``
217
216
- .. code-block:: python
218
217
219
- count = await collection.estimated_document_count()
218
+ await collection.estimated_document_count()
220
219
221
220
* - ``count_documents()``
222
221
- .. code-block:: python
223
222
224
- count = await collection.count_documents(...)
223
+ await collection.count_documents(...)
225
224
226
225
* - ``create_index()``
227
226
- .. code-block:: python
@@ -246,17 +245,17 @@ Collection Methods
246
245
* - ``list_indexes()``
247
246
- .. code-block:: python
248
247
249
- indexes = await collection.list_indexes()
248
+ await collection.list_indexes()
250
249
251
250
* - ``index_information()``
252
251
- .. code-block:: python
253
252
254
- info = await collection.index_information()
253
+ await collection.index_information()
255
254
256
255
* - ``list_search_indexes()``
257
256
- .. code-block:: python
258
257
259
- indexes = await collection.list_search_indexes()
258
+ await collection.list_search_indexes()
260
259
261
260
* - ``create_search_index()``
262
261
- .. code-block:: python
@@ -281,19 +280,19 @@ Collection Methods
281
280
* - ``options()``
282
281
- .. code-block:: python
283
282
284
- options = await collection.options()
283
+ await collection.options()
285
284
286
285
* - ``aggregate()``
287
286
- .. code-block:: python
288
287
289
288
async for doc in await collection.aggregate(...):
290
- ...
289
+ ...
291
290
292
291
* - ``aggregate_raw_batches()``
293
292
- .. code-block:: python
294
293
295
294
async for batch in await collection.aggregate_raw_batches(...):
296
- ...
295
+ ...
297
296
298
297
* - ``rename()``
299
298
- .. code-block:: python
@@ -303,22 +302,22 @@ Collection Methods
303
302
* - ``distinct()``
304
303
- .. code-block:: python
305
304
306
- distinct_values = await collection.distinct(...)
305
+ await collection.distinct(...)
307
306
308
307
* - ``find_one_and_delete()``
309
308
- .. code-block:: python
310
309
311
- result = await collection.find_one_and_delete(...)
310
+ await collection.find_one_and_delete(...)
312
311
313
312
* - ``find_one_and_replace()``
314
313
- .. code-block:: python
315
314
316
- result = await collection.find_one_and_replace(...)
315
+ await collection.find_one_and_replace(...)
317
316
318
317
* - ``find_one_and_update()``
319
318
- .. code-block:: python
320
319
321
- result = await collection.find_one_and_update(...)
320
+ await collection.find_one_and_update(...)
322
321
323
322
Migrate From Motor
324
323
------------------
@@ -340,8 +339,8 @@ read and write operations in Motor compared to {+driver-async+}:
340
339
from pymongo import AsyncMongoClient
341
340
342
341
343
- The following section shows the method signature changes that you must make when
344
- migrating from Motor to the {+driver-async+} driver.
342
+ The following section shows the method signature changes that you must implement
343
+ in your application when migrating from Motor to the {+driver-async+} driver.
345
344
346
345
Method Signature Changes
347
346
~~~~~~~~~~~~~~~~~~~~~~~~
0 commit comments