7272Connection
7373----------
7474
75+ The following sections describe how to connect to different targets, such as a local
76+ instance of MongoDB or a cloud-hosted instance on Atlas.
77+
7578Local Deployment
7679~~~~~~~~~~~~~~~~
7780
81+ The following code shows how to connect the connection string to connect to a local
82+ MongoDB deployment. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to
83+ see the corresponding code:
84+
7885.. tabs::
7986
8087 .. tab:: Synchronous
@@ -96,6 +103,10 @@ Local Deployment
96103Atlas
97104~~~~~
98105
106+ The following code shows the connection string to connect to a deployment hosted on
107+ Atlas. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the
108+ corresponding code:
109+
99110.. tabs::
100111
101112 .. tab:: Synchronous
@@ -105,7 +116,7 @@ Atlas
105116
106117 uri = "<Atlas connection string>"
107118 client = MongoClient(uri, server_api=pymongo.server_api.ServerApi(
108- version="1", strict=True, deprecation_errors=True))
119+ version="1", strict=True, deprecation_errors=True))
109120
110121 .. tab:: Asynchronous
111122 :tabid: async
@@ -114,11 +125,15 @@ Atlas
114125
115126 uri = "<Atlas connection string>"
116127 client = AsyncMongoClient(uri, server_api=pymongo.server_api.ServerApi(
117- version="1", strict=True, deprecation_errors=True))
128+ version="1", strict=True, deprecation_errors=True))
118129
119130Replica Set
120131~~~~~~~~~~~
121132
133+ The following code shows the connection string to connect to a replica set. Select the
134+ :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding
135+ code:
136+
122137.. tabs::
123138
124139 .. tab:: Synchronous
@@ -140,6 +155,9 @@ Replica Set
140155Network Compression
141156-------------------
142157
158+ The following sections describe how to connect to MongoDB while specifying network
159+ compression algorithms.
160+
143161Compression Algorithms
144162~~~~~~~~~~~~~~~~~~~~~~
145163
@@ -151,6 +169,8 @@ To learn more about specifying compression algorithms, see
151169zlib Compression Level
152170~~~~~~~~~~~~~~~~~~~~~~
153171
172+ The following tabs demonstrate how to specify a compression level for the ``zlib`` compressor:
173+
154174.. tabs::
155175
156176 .. tab:: MongoClient
@@ -159,8 +179,8 @@ zlib Compression Level
159179 .. code-block:: python
160180
161181 client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
162- compressors = "zlib",
163- zlibCompressionLevel=<zlib compression level>)
182+ compressors = "zlib",
183+ zlibCompressionLevel=<zlib compression level>)
164184
165185 .. tab:: Connection String
166186 :tabid: connectionstring
@@ -178,17 +198,17 @@ zlib Compression Level
178198 .. code-block:: python
179199
180200 client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
181- compressors = "zlib",
182- zlibCompressionLevel=<zlib compression level>)
201+ compressors = "zlib",
202+ zlibCompressionLevel=<zlib compression level>)
183203
184204 .. tab:: Connection String (Asynchronous)
185205 :tabid: connectionstring-async
186206
187207 .. code-block:: python
188208
189209 uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
190- "compressors=zlib"
191- "zlibCompressionLevel=<zlib compression level>")
210+ "compressors=zlib"
211+ "zlibCompressionLevel=<zlib compression level>")
192212 client = pymongo.AsyncMongoClient(uri)
193213
194214To learn more about setting the zlib compression level, see
@@ -197,6 +217,10 @@ To learn more about setting the zlib compression level, see
197217Server Selection
198218----------------
199219
220+ The following code shows a connection string that specifies a server selection function.
221+ Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding
222+ code:
223+
200224.. tabs::
201225
202226 .. tab:: Synchronous
@@ -205,22 +229,26 @@ Server Selection
205229 .. code-block:: python
206230
207231 client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
208- server_selector=<selector function>)
232+ server_selector=<selector function>)
209233
210234 .. tab:: Asynchronous
211235 :tabid: async
212236
213237 .. code-block:: python
214238
215239 client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
216- server_selector=<selector function>)
240+ server_selector=<selector function>)
217241
218242To learn more about customizing server selection, see
219243:ref:`pymongo-server-selection`.
220244
221245{+stable-api+}
222246--------------
223247
248+ The following code shows how to specify {+stable-api+} settings for a connection.Select the
249+ :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding
250+ code:
251+
224252.. tabs::
225253
226254 .. tab:: Synchronous
@@ -231,7 +259,7 @@ To learn more about customizing server selection, see
231259 from pymongo.server_api import ServerApi
232260
233261 client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>",
234- server_api=ServerApi("<{+stable-api+} version>"))
262+ server_api=ServerApi("<{+stable-api+} version>"))
235263
236264 .. tab:: Asynchronous
237265 :tabid: async
@@ -241,7 +269,7 @@ To learn more about customizing server selection, see
241269 from pymongo.server_api import ServerApi
242270
243271 client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>",
244- server_api=ServerApi("<{+stable-api+} version>"))
272+ server_api=ServerApi("<{+stable-api+} version>"))
245273
246274To learn more about the {+stable-api+}, see :ref:`pymongo-stable-api`.
247275
@@ -251,6 +279,8 @@ Limit Server Execution Time
251279timeout Block
252280~~~~~~~~~~~~~
253281
282+ The following code shows how to set a client-side timeout by using the ``timeout()`` method:
283+
254284.. code-block:: python
255285
256286 with pymongo.timeout(<timeout length>):
@@ -261,6 +291,9 @@ To learn more about client-side timeouts, see :ref:`pymongo-csot`.
261291timeoutMS Connection Option
262292~~~~~~~~~~~~~~~~~~~~~~~~~~~
263293
294+ The following tabs demonstrate how to set a client-side timeout by using the ``timeoutMS``
295+ connection option:
296+
264297.. tabs::
265298
266299 .. tab:: MongoClient
@@ -269,7 +302,7 @@ timeoutMS Connection Option
269302 .. code-block:: python
270303
271304 client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname@:<port>",
272- timeoutMS=<timeout length>)
305+ timeoutMS=<timeout length>)
273306
274307 .. tab:: Connection String
275308 :tabid: connectionstring
@@ -285,7 +318,7 @@ timeoutMS Connection Option
285318 .. code-block:: python
286319
287320 client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname@:<port>",
288- timeoutMS=<timeout length>)
321+ timeoutMS=<timeout length>)
289322
290323 .. tab:: Connection String (Asynchronous)
291324 :tabid: connectionstring-async
0 commit comments