File tree Expand file tree Collapse file tree 2 files changed +38
-4
lines changed Expand file tree Collapse file tree 2 files changed +38
-4
lines changed Original file line number Diff line number Diff line change @@ -10,11 +10,21 @@ Run a Sample Query
10
10
11
11
.. step:: Create your {+driver-short+} Application
12
12
13
- Copy and paste the following code into the ``quickstart.py`` file in your application:
13
+ Copy and paste the following code into the ``quickstart.py`` file in your application.
14
+ Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding
15
+ code:
14
16
15
- .. literalinclude:: /includes/get-started/connect-and-query.py
16
- :language: python
17
- :copyable:
17
+ .. tabs::
18
+
19
+ .. tab:: Synchronous
20
+ :tabid: sync
21
+
22
+ .. literalinclude:: /includes/get-started/connect-and-query.py
23
+
24
+ .. tab:: Asynchronous
25
+ :tabid: async
26
+
27
+ .. literalinclude:: /includes/get-started/connect-and-query-async.py
18
28
19
29
.. step:: Assign the Connection String
20
30
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