File tree Expand file tree Collapse file tree 2 files changed +42
-4
lines changed Expand file tree Collapse file tree 2 files changed +42
-4
lines changed Original file line number Diff line number Diff line change @@ -221,11 +221,25 @@ Connect to MongoDB
221
221
222
222
.. step:: Create your {+driver-short+} application
223
223
224
- Copy and paste the following code into the ``quickstart.py`` file in your application:
224
+ Copy and paste the following code into the ``quickstart.py`` file in your application.
225
+ Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the
226
+ corresponding code:
227
+
228
+ .. tabs::
229
+
230
+ .. tab:: Synchronous
231
+ :tabid: sync
232
+
233
+ .. literalinclude:: /includes/get-started/connect-and-query.py
234
+ :language: python
235
+ :copyable:
236
+
237
+ .. tab:: Asynchronous
238
+ :tabid: async
225
239
226
- .. literalinclude:: /includes/get-started/connect-and-query.py
227
- :language: python
228
- :copyable:
240
+ .. literalinclude:: /includes/get-started/connect-and-query-async .py
241
+ :language: python
242
+ :copyable:
229
243
230
244
.. step:: Assign the connection string
231
245
Original file line number Diff line number Diff line change
1
+ import asyncio
2
+ from pymongo import AsyncMongoClient
3
+
4
+ async def main ():
5
+ uri = "<connection string URI>"
6
+ client = AsyncMongoClient (uri )
7
+
8
+ try :
9
+ database = client .get_database ("sample_mflix" )
10
+ movies = database .get_collection ("movies" )
11
+
12
+ # Query for a movie that has the title 'Back to the Future'
13
+ query = { "title" : "Back to the Future" }
14
+ movie = await movies .find_one (query )
15
+
16
+ print (movie )
17
+
18
+ await client .close ()
19
+
20
+ except Exception as e :
21
+ raise Exception ("Unable to find the document due to the following error: " , e )
22
+
23
+ # Run the async function
24
+ asyncio .run (main ())
You can’t perform that action at this time.
0 commit comments