diff --git a/source/connect.txt b/source/connect.txt index ef2c17b4..2d20ac93 100644 --- a/source/connect.txt +++ b/source/connect.txt @@ -46,43 +46,118 @@ the relevant values for your MongoDB deployment. .. include:: /includes/usage-examples/sample-app-intro.rst -.. literalinclude:: /includes/usage-examples/connect-sample-app.py - :language: python - :copyable: true - :linenos: - :emphasize-lines: 4-6 +Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding +code: + +.. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/usage-examples/connect-sample-app.py + :language: python + :copyable: true + :linenos: + :emphasize-lines: 4-6 + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/usage-examples/connect-sample-app-async.py + :language: python + :copyable: true + :linenos: + :emphasize-lines: 6-8 Connection ---------- +The following sections describe how to connect to different targets, such as a local +instance of MongoDB or a cloud-hosted instance on Atlas. + Local Deployment ~~~~~~~~~~~~~~~~ -.. code-block:: python +The following code shows how to connect the connection string to connect to a local +MongoDB deployment. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to +see the corresponding code: - uri = "mongodb://localhost:27017/" - client = MongoClient(uri) +.. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. code-block:: python + + uri = "mongodb://localhost:27017/" + client = MongoClient(uri) + + .. tab:: Asynchronous + :tabid: async + + .. code-block:: python + + uri = "mongodb://localhost:27017/" + client = AsyncMongoClient(uri) Atlas ~~~~~ -.. code-block:: python +The following code shows the connection string to connect to a deployment hosted on +Atlas. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the +corresponding code: - uri = "" - client = MongoClient(uri, server_api=pymongo.server_api.ServerApi( - version="1", strict=True, deprecation_errors=True)) +.. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. code-block:: python + + uri = "" + client = MongoClient(uri, server_api=pymongo.server_api.ServerApi( + version="1", strict=True, deprecation_errors=True)) + + .. tab:: Asynchronous + :tabid: async + + .. code-block:: python + + uri = "" + client = AsyncMongoClient(uri, server_api=pymongo.server_api.ServerApi( + version="1", strict=True, deprecation_errors=True)) Replica Set ~~~~~~~~~~~ -.. code-block:: python +The following code shows the connection string to connect to a replica set. Select the +:guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding +code: + +.. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. code-block:: python + + uri = "mongodb://:/?replicaSet=" + client = MongoClient(uri) - uri = "mongodb://:/?replicaSet=" - client = MongoClient(uri) + .. tab:: Asynchronous + :tabid: async + + .. code-block:: python + + uri = "mongodb://:/?replicaSet=" + client = AsyncMongoClient(uri) Network Compression ------------------- +The following sections describe how to connect to MongoDB while specifying network +compression algorithms. + Compression Algorithms ~~~~~~~~~~~~~~~~~~~~~~ @@ -94,6 +169,8 @@ To learn more about specifying compression algorithms, see zlib Compression Level ~~~~~~~~~~~~~~~~~~~~~~ +The following tabs demonstrate how to specify a compression level for the ``zlib`` compressor: + .. tabs:: .. tab:: MongoClient @@ -102,8 +179,8 @@ zlib Compression Level .. code-block:: python client = pymongo.MongoClient("mongodb://:@:", - compressors = "zlib", - zlibCompressionLevel=) + compressors = "zlib", + zlibCompressionLevel=) .. tab:: Connection String :tabid: connectionstring @@ -115,16 +192,52 @@ zlib Compression Level "zlibCompressionLevel=") client = pymongo.MongoClient(uri) + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:@:", + compressors = "zlib", + zlibCompressionLevel=) + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = ("mongodb://:@:/?" + "compressors=zlib" + "zlibCompressionLevel=") + client = pymongo.AsyncMongoClient(uri) + To learn more about setting the zlib compression level, see :ref:`pymongo-enable-compression` in the Network Compression guide. Server Selection ---------------- -.. code-block:: python +The following code shows a connection string that specifies a server selection function. +Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding +code: - client = pymongo.MongoClient("mongodb://:@:", - server_selector=) +.. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. code-block:: python + + client = pymongo.MongoClient("mongodb://:@:", + server_selector=) + + .. tab:: Asynchronous + :tabid: async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:@:", + server_selector=) To learn more about customizing server selection, see :ref:`pymongo-server-selection`. @@ -132,12 +245,31 @@ To learn more about customizing server selection, see {+stable-api+} -------------- -.. code-block:: python +The following code shows how to specify {+stable-api+} settings for a connection.Select the +:guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding +code: - from pymongo.server_api import ServerApi +.. tabs:: - client = pymongo.MongoClient("mongodb://:@", - server_api=ServerApi("<{+stable-api+} version>")) + .. tab:: Synchronous + :tabid: sync + + .. code-block:: python + + from pymongo.server_api import ServerApi + + client = pymongo.MongoClient("mongodb://:@", + server_api=ServerApi("<{+stable-api+} version>")) + + .. tab:: Asynchronous + :tabid: async + + .. code-block:: python + + from pymongo.server_api import ServerApi + + client = pymongo.AsyncMongoClient("mongodb://:@", + server_api=ServerApi("<{+stable-api+} version>")) To learn more about the {+stable-api+}, see :ref:`pymongo-stable-api`. @@ -147,6 +279,8 @@ Limit Server Execution Time timeout Block ~~~~~~~~~~~~~ +The following code shows how to set a client-side timeout by using the ``timeout()`` method: + .. code-block:: python with pymongo.timeout(): @@ -157,6 +291,9 @@ To learn more about client-side timeouts, see :ref:`pymongo-csot`. timeoutMS Connection Option ~~~~~~~~~~~~~~~~~~~~~~~~~~~ +The following tabs demonstrate how to set a client-side timeout by using the ``timeoutMS`` +connection option: + .. tabs:: .. tab:: MongoClient @@ -165,7 +302,7 @@ timeoutMS Connection Option .. code-block:: python client = pymongo.MongoClient("mongodb://:@", - timeoutMS=) + timeoutMS=) .. tab:: Connection String :tabid: connectionstring @@ -175,5 +312,20 @@ timeoutMS Connection Option uri = "mongodb://:@/?timeoutMS=" client = pymongo.MongoClient(uri) -To learn more about client-side timeouts, see :ref:`pymongo-csot`. + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:@", + timeoutMS=) + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + .. code-block:: python + + uri = "mongodb://:@/?timeoutMS=" + client = pymongo.AsyncMongoClient(uri) + +To learn more about client-side timeouts, see :ref:`pymongo-csot`. diff --git a/source/connect/connection-options.txt b/source/connect/connection-options.txt index 4aed4b87..585a0d89 100644 --- a/source/connect/connection-options.txt +++ b/source/connect/connection-options.txt @@ -42,12 +42,26 @@ Using the Connection URI If you pass a connection URI to the ``MongoClient`` constructor, you can include connection options in the string as ``=`` pairs. In the following example, the connection URI contains the ``connectTimeoutMS`` option with a value of ``60000`` -and the ``tls`` option with a value of ``true``: +and the ``tls`` option with a value of ``true``. Select the :guilabel:`Synchronous` or +:guilabel:`Asynchronous` tab to see the corresponding code: -.. code-block:: python +.. tabs:: - uri = "mongodb://:/?connectTimeoutMS=60000&tls=true" - client = pymongo.MongoClient(uri) + .. tab:: Synchronous + :tabid: sync + + .. code-block:: python + + uri = "mongodb://:/?connectTimeoutMS=60000&tls=true" + client = pymongo.MongoClient(uri) + + .. tab:: Asynchronous + :tabid: async + + .. code-block:: python + + uri = "mongodb://:/?connectTimeoutMS=60000&tls=true" + client = pymongo.AsyncMongoClient(uri) .. _pymongo-mongo-client-settings: @@ -59,12 +73,26 @@ instead of including them in your connection URI. Configuring the connection this way makes it easier to change settings at runtime and helps you catch errors during compilation. The following example shows how to use the ``MongoClient`` constructor to set -connection options: +connection options. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to +see the corresponding code: + +.. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. code-block:: python + + uri = "mongodb://:" + client = pymongo.MongoClient(uri, connectTimeoutMS=60000, tls=True) + + .. tab:: Asynchronous + :tabid: async -.. code-block:: python + .. code-block:: python - uri = "mongodb://:" - client = pymongo.MongoClient(uri, connectTimeoutMS=60000, tls=True) + uri = "mongodb://:" + client = pymongo.AsyncMongoClient(uri, connectTimeoutMS=60000, tls=True) Connection Options ------------------ diff --git a/source/connect/connection-options/connection-pools.txt b/source/connect/connection-options/connection-pools.txt index 2dfde47f..b5129b3e 100644 --- a/source/connect/connection-options/connection-pools.txt +++ b/source/connect/connection-options/connection-pools.txt @@ -110,18 +110,43 @@ your connection URI: | **Connection URI Example**: ``waitQueueTimeoutMS=100000`` The following code creates a client with a maximum connection pool size of ``50`` by using the -``maxPoolSize`` parameter: +``maxPoolSize`` parameter. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` +tab to see the corresponding code: -.. code-block:: python +.. tabs:: - client = MongoClient(host, port, maxPoolSize=50) + .. tab:: Synchronous + :tabid: sync + + .. code-block:: python + + client = MongoClient(host, port, maxPoolSize=50) + + .. tab:: Asynchronous + :tabid: async + + .. code-block:: python + + client = AsyncMongoClient(host, port, maxPoolSize=50) The following code creates a client with the same configuration as the preceding example, but uses a connection URI: -.. code-block:: python +.. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. code-block:: python + + client = MongoClient(host, port, maxPoolSize=50) + + .. tab:: Asynchronous + :tabid: async + + .. code-block:: python - client = MongoClient("mongodb://:/?maxPoolSize=50") + client = AsyncMongoClient(host, port, maxPoolSize=50) Additional Information ---------------------- diff --git a/source/connect/connection-options/network-compression.txt b/source/connect/connection-options/network-compression.txt index 6171ba79..139a012e 100644 --- a/source/connect/connection-options/network-compression.txt +++ b/source/connect/connection-options/network-compression.txt @@ -93,6 +93,27 @@ The following code example specifies the ``zlib`` compression algorithm and a va "zlibCompressionLevel=1") client = pymongo.MongoClient(uri) + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + :emphasize-lines: 2-3 + + client = pymongo.AsyncMongoClient("mongodb://:@:", + compressors = "zlib", + zlibCompressionLevel=1) + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + :emphasize-lines: 2-3 + + uri = ("mongodb://:@:/?" + "compressors=zlib" + "zlibCompressionLevel=1") + client = pymongo.AsyncMongoClient(uri) + API Documentation ----------------- diff --git a/source/connect/connection-options/server-selection.txt b/source/connect/connection-options/server-selection.txt index ddff92c4..9a2faf5c 100644 --- a/source/connect/connection-options/server-selection.txt +++ b/source/connect/connection-options/server-selection.txt @@ -117,12 +117,27 @@ the list of servers originally passed as an argument: return servers Finally, instruct {+driver-short+} to use your function. To do so, call the ``MongoClient`` -constructor and pass the ``server_selector`` argument with your function name as the value: +constructor and pass the ``server_selector`` argument with your function name as the value. +Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding +code: -.. code-block:: python +.. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. code-block:: python + + client = pymongo.MongoClient("mongodb://:@:", + server_selector=prefer_local) + + .. tab:: Asynchronous + :tabid: async + + .. code-block:: python - client = pymongo.MongoClient("mongodb://:@:", - server_selector=prefer_local) + client = pymongo.AsyncMongoClient("mongodb://:@:", + server_selector=prefer_local) API Documentation ----------------- diff --git a/source/connect/connection-options/stable-api.txt b/source/connect/connection-options/stable-api.txt index c6ef8b3c..03418182 100644 --- a/source/connect/connection-options/stable-api.txt +++ b/source/connect/connection-options/stable-api.txt @@ -48,16 +48,34 @@ To enable the {+stable-api+}, perform the following steps: #. Construct a ``MongoClient`` object, passing in your ``ServerApi`` object for the ``server_api`` argument. -The following code example shows how to specify {+stable-api+} version 1: +The following code example shows how to specify {+stable-api+} version 1. Select the +:guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding code: -.. code-block:: python - :emphasize-lines: 5 +.. tabs:: - from pymongo import MongoClient - from pymongo.server_api import ServerApi + .. tab:: Synchronous + :tabid: sync - client = MongoClient("mongodb://:@", - server_api=ServerApi("1")) + .. code-block:: python + :emphasize-lines: 5 + + from pymongo import MongoClient + from pymongo.server_api import ServerApi + + client = MongoClient("mongodb://:@", + server_api=ServerApi("1")) + + .. tab:: Asynchronous + :tabid: async + + .. code-block:: python + :emphasize-lines: 5 + + from pymongo import AsyncMongoClient + from pymongo.server_api import ServerApi + + client = AsyncMongoClient("mongodb://:@", + server_api=ServerApi("1")) Once you create a ``MongoClient`` instance with a specified API version, all commands you run with the client use the specified @@ -93,18 +111,38 @@ parameters to customize the behavior of the {+stable-api+}. | Default: **False** The following code example shows how you can use these parameters when constructing a -``ServerApi`` object: +``ServerApi`` object. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to +see the corresponding code: + +.. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. code-block:: python + :emphasize-lines: 5-7 + + from pymongo import MongoClient + from pymongo.server_api import ServerApi + + client = MongoClient("mongodb://:@", + server_api=ServerApi("1", + strict=True, + deprecation_errors=True)) + + .. tab:: Asynchronous + :tabid: async -.. code-block:: python - :emphasize-lines: 5-7 + .. code-block:: python + :emphasize-lines: 5-7 - from pymongo import MongoClient - from pymongo.server_api import ServerApi + from pymongo import AsyncMongoClient + from pymongo.server_api import ServerApi - client = MongoClient("mongodb://:@", - server_api=ServerApi("1", - strict=True, - deprecation_errors=True)) + client = AsyncMongoClient("mongodb://:@", + server_api=ServerApi("1", + strict=True, + deprecation_errors=True)) Troubleshooting --------------- diff --git a/source/connect/connection-targets.txt b/source/connect/connection-targets.txt index e6f25ee1..e969cb47 100644 --- a/source/connect/connection-targets.txt +++ b/source/connect/connection-targets.txt @@ -46,10 +46,23 @@ To learn more about the {+stable-api+} feature, see the :ref:`{+stable-api+} pag `. The following code shows how to use {+driver-short+} to connect to an Atlas cluster. The -code also uses the ``server_api`` option to specify a {+stable-api+} version. +code also uses the ``server_api`` option to specify a {+stable-api+} version. Select the +:guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding +code: -.. literalinclude:: /includes/connect/atlas_connection.py - :language: python +.. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/connect/atlas_connection.py + :language: python + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/connect/atlas-connection-async.py + :language: python Local Deployments ----------------- @@ -59,14 +72,30 @@ default, the ``mongod`` process runs on port 27017, though you can customize thi your deployment. The following code shows how to use {+driver-short+} to connect to a local MongoDB -deployment: +deployment. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the +corresponding code: -.. code-block:: python +.. tabs:: - from pymongo import MongoClient + .. tab:: Synchronous + :tabid: sync + + .. code-block:: python + + from pymongo import MongoClient + + uri = "mongodb://localhost:27017/" + client = MongoClient(uri) - uri = "mongodb://localhost:27017/" - client = MongoClient(uri) + .. tab:: Asynchronous + :tabid: async + + .. code-block:: python + + from pymongo import AsyncMongoClient + + uri = "mongodb://localhost:27017/" + client = AsyncMongoClient(uri) Replica Sets ------------ @@ -75,13 +104,28 @@ To connect to a replica set, specify the hostnames (or IP addresses) and port numbers of the replica-set members in your connection string. The following code shows how to use {+driver-short+} to connect to a replica set -that contains three hosts: +that contains three hosts. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` +tab to see the corresponding code: -.. code-block:: python +.. tabs:: + + .. tab:: Synchronous + :tabid: sync - from pymongo import MongoClient + .. code-block:: python + + from pymongo import MongoClient + + client = MongoClient("mongodb://host1:27017,host2:27017,host3:27017") - client = MongoClient("mongodb://host1:27017,host2:27017,host3:27017") + .. tab:: Asynchronous + :tabid: async + + .. code-block:: python + + from pymongo import AsyncMongoClient + + client = AsyncMongoClient("mongodb://host1:27017,host2:27017,host3:27017") If you aren't able to provide a full list of hosts in the replica set, you can specify one or more of the hosts in the replica set and instruct {+driver-short+} to @@ -94,14 +138,30 @@ automatic discovery, perform one of the following actions: In the following example, the driver uses a sample connection URI to connect to the MongoDB replica set ``sampleRS``, which is running on port ``27017`` of three different -hosts, including ``host1``: +hosts, including ``host1``. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` +tab to see the corresponding code: -.. code-block:: python +.. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. code-block:: python + + from pymongo import MongoClient - from pymongo import MongoClient + uri = "mongodb://host1:27017/?replicaSet=sampleRS" + client = MongoClient(uri) + + .. tab:: Asynchronous + :tabid: async - uri = "mongodb://host1:27017/?replicaSet=sampleRS" - client = MongoClient(uri) + .. code-block:: python + + from pymongo import AsyncMongoClient + + uri = "mongodb://host1:27017/?replicaSet=sampleRS" + client = AsyncMongoClient(uri) {+driver-short+} evenly load balances operations across deployments that are reachable within the client's ``localThresholdMS`` value. To learn more about how {+driver-short+} load @@ -151,6 +211,29 @@ option to ``True``. You can do this in two ways: by passing an argument to the "directConnection=true") client = MongoClient(uri) + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + :emphasize-lines: 4 + + from pymongo import AsyncMongoClient + + client = AsyncMongoClient("mongodb://:", + directConnection=True) + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + :emphasize-lines: 4 + + from pymongo import AsyncMongoClient + + uri = ("mongodb://:/?" + "directConnection=true") + client = AsyncMongoClient(uri) + DNS Service Discovery --------------------- diff --git a/source/connect/mongoclient.txt b/source/connect/mongoclient.txt index 444a6d83..a577d2be 100644 --- a/source/connect/mongoclient.txt +++ b/source/connect/mongoclient.txt @@ -97,6 +97,16 @@ instance on port ``27017`` of ``localhost``: uri = "mongodb://localhost:27017/" client = MongoClient(uri) +If you are working with an asynchronous application, use the ``AsyncMongoClient`` class. +The following example shows how to create an ``AsyncMongoClient`` object: + +.. code-block:: python + + from pymongo import AsyncMongoClient + + uri = "mongodb://localhost:27017/" + client = AsyncMongoClient(uri) + The following table describes the positional parameters that the ``MongoClient()`` constructor accepts. All parameters are optional. @@ -257,37 +267,92 @@ Type Hints .. include:: /includes/type-hints/intro.rst To use type hints in your {+driver-short+} application, you must add a type annotation to your -``MongoClient`` object, as shown in the following example: +``MongoClient`` object, as shown in the following example. Select the :guilabel:`Synchronous` +or :guilabel:`Asynchronous` tab to see the corresponding code: -.. code-block:: python +.. tabs:: + + .. tab:: Synchronous + :tabid: sync - client: MongoClient = MongoClient() + .. code-block:: python + + from pymongo import MongoClient + + client: MongoClient = MongoClient() + + .. tab:: Asynchronous + :tabid: async + + .. code-block:: python + + from pymongo import AsyncMongoClient + + client: AsyncMongoClient = AsyncMongoClient() For more accurate type information, you can include the generic document type ``Dict[str, Any]`` in your type annotation. This data type matches all documents in MongoDB. -The following example shows how to include this data type in your type annotation: +The following example shows how to include this data type in your type annotation. +Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding +code: -.. code-block:: python +.. tabs:: + + .. tab:: Synchronous + :tabid: sync - from typing import Any, Dict - client: MongoClient[Dict[str, Any]] = MongoClient() + .. code-block:: python + + from pymongo import MongoClient + from typing import Any, Dict + + client: MongoClient[Dict[str, Any]] = MongoClient() + + .. tab:: Asynchronous + :tabid: async + + .. code-block:: python + + from pymongo import AsyncMongoClient + from typing import Any, Dict + + client: AsyncMongoClient[Dict[str, Any]] = AsyncMongoClient() If all the documents that you are working with correspond to a single custom type, you can specify the custom type as a type hint for your ``MongoClient`` object. This provides more accurate type information than the generic ``Dict[str, Any]`` type. The following example shows how to specify the ``Movie`` type as a type hint for a -``MongoClient`` object: +``MongoClient`` object. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab +to see the corresponding code: -.. code-block:: python +.. tabs:: + + .. tab:: Synchronous + :tabid: sync - from typing import TypedDict + .. code-block:: python - class Movie(TypedDict): - name: str - year: int - - client: MongoClient[Movie] = MongoClient() + from typing import TypedDict + + class Movie(TypedDict): + name: str + year: int + + client: MongoClient[Movie] = MongoClient() + + .. tab:: Asynchronous + :tabid: async + + .. code-block:: python + + from typing import TypedDict + + class Movie(TypedDict): + name: str + year: int + + client: AsyncMongoClient[Movie] = AsyncMongoClient() Troubleshooting --------------- @@ -363,4 +428,5 @@ API Documentation To learn more about creating a ``MongoClient`` object in {+driver-short+}, see the following API documentation: -- `MongoClient <{+api-root+}pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`__ \ No newline at end of file +- `MongoClient <{+api-root+}pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`__ +- `AsyncMongoClient <{+api-root+}pymongo/asynchronous/mongo_client.html#pymongo.asynchronous.mongo_client.AsyncMongoClient>`__ \ No newline at end of file diff --git a/source/data-formats/extended-json.txt b/source/data-formats/extended-json.txt index 5a3ad056..2371dea9 100644 --- a/source/data-formats/extended-json.txt +++ b/source/data-formats/extended-json.txt @@ -189,7 +189,7 @@ class with subtype 0. The following code examples show how {+driver-short+} decodes JSON binary instances with subtype 0. Select the :guilabel:`Python 2` or :guilabel:`Python 3` tab to view the -corresponding code. +corresponding code: .. tabs:: diff --git a/source/includes/connect/atlas-connection-async.py b/source/includes/connect/atlas-connection-async.py new file mode 100644 index 00000000..d4141a7e --- /dev/null +++ b/source/includes/connect/atlas-connection-async.py @@ -0,0 +1,22 @@ +import asyncio +from pymongo import AsyncMongoClient +from pymongo.server_api import ServerApi + +async def main(): + # Replace the placeholder with your Atlas connection string + uri = "" + + # Create a MongoClient with a MongoClientOptions object to set the Stable API version + client = AsyncMongoClient(uri, server_api=ServerApi( + version='1', strict=True, deprecation_errors=True)) + + try: + # Send a ping to confirm a successful connection + await client.admin.command({'ping': 1}) + print("Pinged your deployment. You successfully connected to MongoDB!") + + finally: + # Ensures that the client will close when you finish/error + await client.close() + +asyncio.run(main()) \ No newline at end of file diff --git a/source/includes/connect/compression-tabs.rst b/source/includes/connect/compression-tabs.rst index d4db3a23..59cd616f 100644 --- a/source/includes/connect/compression-tabs.rst +++ b/source/includes/connect/compression-tabs.rst @@ -1,3 +1,5 @@ +The following tabs demonstrate how to specify all available compressors while connecting to MongoDB: + .. tabs:: .. tab:: MongoClient @@ -15,4 +17,21 @@ uri = ("mongodb://:@:/?" "compressors=snappy,zstd,zlib") - client = pymongo.MongoClient(uri) \ No newline at end of file + client = pymongo.MongoClient(uri) + + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:@:", + compressors = "snappy,zstd,zlib") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = ("mongodb://:@:/?" + "compressors=snappy,zstd,zlib") + client = pymongo.AsyncMongoClient(uri) \ No newline at end of file diff --git a/source/includes/usage-examples/connect-sample-app-async.py b/source/includes/usage-examples/connect-sample-app-async.py new file mode 100644 index 00000000..1328c0c7 --- /dev/null +++ b/source/includes/usage-examples/connect-sample-app-async.py @@ -0,0 +1,21 @@ +import asyncio +from pymongo import AsyncMongoClient + +async def main(): + try: + # start example code here + + # end example code here + + await client.admin.command("ping") + print("Connected successfully") + + # other application code + + await client.close() + + except Exception as e: + raise Exception( + "The following error occurred: ", e) + +asyncio.run(main()) \ No newline at end of file