Skip to content

Commit a911ae5

Browse files
committed
DOCSP-43214: Get Started async example
1 parent bdc7b75 commit a911ae5

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

source/get-started.txt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,25 @@ Connect to MongoDB
221221

222222
.. step:: Create your {+driver-short+} application
223223

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
225239

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

230244
.. step:: Assign the connection string
231245

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)