Skip to content

Commit fc718e4

Browse files
committed
DOCSP-43214: Get Started async example
1 parent a235231 commit fc718e4

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

source/get-started/run-sample-query.txt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,21 @@ Run a Sample Query
1010

1111
.. step:: Create your {+driver-short+} Application
1212

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:
1416

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
1828

1929
.. step:: Assign the Connection String
2030

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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())

0 commit comments

Comments
 (0)