diff --git a/source/get-started/run-sample-query.txt b/source/get-started/run-sample-query.txt index fa12ac4e..b6778f41 100644 --- a/source/get-started/run-sample-query.txt +++ b/source/get-started/run-sample-query.txt @@ -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 diff --git a/source/includes/get-started/connect-and-query-async.py b/source/includes/get-started/connect-and-query-async.py new file mode 100644 index 00000000..a9c924ae --- /dev/null +++ b/source/includes/get-started/connect-and-query-async.py @@ -0,0 +1,24 @@ +import asyncio +from pymongo import AsyncMongoClient + +async def main(): + 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())