Skip to content

DOCSP-43214: Get Started async example #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions source/get-started/run-sample-query.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ Run a Sample Query

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

Copy and paste the following code into the ``quickstart.py`` file in your application:
Copy and paste the following code into the ``quickstart.py`` file in your application.
Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding
code:

.. literalinclude:: /includes/get-started/connect-and-query.py
:language: python
:copyable:
.. tabs::

.. tab:: Synchronous
:tabid: sync

.. literalinclude:: /includes/get-started/connect-and-query.py

.. tab:: Asynchronous
:tabid: async

.. literalinclude:: /includes/get-started/connect-and-query-async.py

.. step:: Assign the Connection String

Expand Down
24 changes: 24 additions & 0 deletions source/includes/get-started/connect-and-query-async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import asyncio
from pymongo import AsyncMongoClient

async def main():
uri = "<connection string URI>"
client = AsyncMongoClient(uri)

try:
database = client.get_database("sample_mflix")
movies = database.get_collection("movies")

# Query for a movie that has the title 'Back to the Future'
query = { "title": "Back to the Future" }
movie = await movies.find_one(query)

print(movie)

await client.close()

except Exception as e:
raise Exception("Unable to find the document due to the following error: ", e)

# Run the async function
asyncio.run(main())
Loading