Skip to content

DOCSP-48161: Async examples for Connect pages #236

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

Merged
merged 5 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
165 changes: 142 additions & 23 deletions source/connect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,39 +46,96 @@ 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
----------

Local Deployment
~~~~~~~~~~~~~~~~

.. code-block:: python
.. tabs::

uri = "mongodb://localhost:27017/"
client = MongoClient(uri)
.. 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
.. tabs::

uri = "<Atlas connection string>"
client = MongoClient(uri, server_api=pymongo.server_api.ServerApi(
version="1", strict=True, deprecation_errors=True))
.. tab:: Synchronous
:tabid: sync

.. code-block:: python

uri = "<Atlas connection string>"
client = MongoClient(uri, server_api=pymongo.server_api.ServerApi(
version="1", strict=True, deprecation_errors=True))

.. tab:: Asynchronous
:tabid: async

.. code-block:: python

uri = "<Atlas connection string>"
client = AsyncMongoClient(uri, server_api=pymongo.server_api.ServerApi(
version="1", strict=True, deprecation_errors=True))

Replica Set
~~~~~~~~~~~

.. code-block:: python
.. tabs::

.. tab:: Synchronous
:tabid: sync

.. code-block:: python

uri = "mongodb://<replica set member>:<port>/?replicaSet=<replica set name>"
client = MongoClient(uri)

uri = "mongodb://<replica set member>:<port>/?replicaSet=<replica set name>"
client = MongoClient(uri)
.. tab:: Asynchronous
:tabid: async

.. code-block:: python

uri = "mongodb://<replica set member>:<port>/?replicaSet=<replica set name>"
client = AsyncMongoClient(uri)

Network Compression
-------------------
Expand Down Expand Up @@ -115,29 +172,76 @@ zlib Compression Level
"zlibCompressionLevel=<zlib compression level>")
client = pymongo.MongoClient(uri)

.. tab:: MongoClient (Asynchronous)
:tabid: mongoclient-async

.. code-block:: python

client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
compressors = "zlib",
zlibCompressionLevel=<zlib compression level>)

.. tab:: Connection String (Asynchronous)
:tabid: connectionstring-async

.. code-block:: python

uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
"compressors=zlib"
"zlibCompressionLevel=<zlib compression level>")
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
.. tabs::

client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
server_selector=<selector function>)
.. tab:: Synchronous
:tabid: sync

.. code-block:: python

client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
server_selector=<selector function>)

.. tab:: Asynchronous
:tabid: async

.. code-block:: python

client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
server_selector=<selector function>)

To learn more about customizing server selection, see
:ref:`pymongo-server-selection`.

{+stable-api+}
--------------

.. code-block:: python
.. tabs::

from pymongo.server_api import ServerApi
.. tab:: Synchronous
:tabid: sync

client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>",
server_api=ServerApi("<{+stable-api+} version>"))
.. code-block:: python

from pymongo.server_api import ServerApi

client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>",
server_api=ServerApi("<{+stable-api+} version>"))

.. tab:: Asynchronous
:tabid: async

.. code-block:: python

from pymongo.server_api import ServerApi

client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>",
server_api=ServerApi("<{+stable-api+} version>"))

To learn more about the {+stable-api+}, see :ref:`pymongo-stable-api`.

Expand Down Expand Up @@ -175,5 +279,20 @@ timeoutMS Connection Option
uri = "mongodb://<db_username>:<db_password>@<hostname:<port>/?timeoutMS=<timeout length>"
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://<db_username>:<db_password>@<hostname@:<port>",
timeoutMS=<timeout length>)

.. tab:: Connection String (Asynchronous)
:tabid: connectionstring-async

.. code-block:: python

uri = "mongodb://<db_username>:<db_password>@<hostname:<port>/?timeoutMS=<timeout length>"
client = pymongo.AsyncMongoClient(uri)

To learn more about client-side timeouts, see :ref:`pymongo-csot`.
44 changes: 36 additions & 8 deletions source/connect/connection-options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ``<name>=<value>`` 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://<hostname>:<port>/?connectTimeoutMS=60000&tls=true"
client = pymongo.MongoClient(uri)
.. tab:: Synchronous
:tabid: sync

.. code-block:: python

uri = "mongodb://<hostname>:<port>/?connectTimeoutMS=60000&tls=true"
client = pymongo.MongoClient(uri)

.. tab:: Asynchronous
:tabid: async

.. code-block:: python

uri = "mongodb://<hostname>:<port>/?connectTimeoutMS=60000&tls=true"
client = pymongo.AsyncMongoClient(uri)

.. _pymongo-mongo-client-settings:

Expand All @@ -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://<hostname>:<port>"
client = pymongo.MongoClient(uri, connectTimeoutMS=60000, tls=True)

.. tab:: Asynchronous
:tabid: async

.. code-block:: python
.. code-block:: python

uri = "mongodb://<hostname>:<port>"
client = pymongo.MongoClient(uri, connectTimeoutMS=60000, tls=True)
uri = "mongodb://<hostname>:<port>"
client = pymongo.AsyncMongoClient(uri, connectTimeoutMS=60000, tls=True)

Connection Options
------------------
Expand Down
35 changes: 30 additions & 5 deletions source/connect/connection-options/connection-pools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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://<host>:<port>/?maxPoolSize=50")
client = AsyncMongoClient(host, port, maxPoolSize=50)

Additional Information
----------------------
Expand Down
21 changes: 21 additions & 0 deletions source/connect/connection-options/network-compression.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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://<db_username>:<db_password>@<hostname>:<port>",
compressors = "zlib",
zlibCompressionLevel=1)

.. tab:: Connection String (Asynchronous)
:tabid: connectionstring-async

.. code-block:: python
:emphasize-lines: 2-3

uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
"compressors=zlib"
"zlibCompressionLevel=1")
client = pymongo.AsyncMongoClient(uri)

API Documentation
-----------------

Expand Down
23 changes: 19 additions & 4 deletions source/connect/connection-options/server-selection.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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://<db_username>:<db_password>@<hostname>:<port>",
server_selector=prefer_local)

.. tab:: Asynchronous
:tabid: async

.. code-block:: python

client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
server_selector=prefer_local)
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
server_selector=prefer_local)

API Documentation
-----------------
Expand Down
Loading
Loading