72
72
Connection
73
73
----------
74
74
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
+
75
78
Local Deployment
76
79
~~~~~~~~~~~~~~~~
77
80
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
+
78
85
.. tabs::
79
86
80
87
.. tab:: Synchronous
@@ -96,6 +103,10 @@ Local Deployment
96
103
Atlas
97
104
~~~~~
98
105
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
+
99
110
.. tabs::
100
111
101
112
.. tab:: Synchronous
@@ -105,7 +116,7 @@ Atlas
105
116
106
117
uri = "<Atlas connection string>"
107
118
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))
109
120
110
121
.. tab:: Asynchronous
111
122
:tabid: async
@@ -114,11 +125,15 @@ Atlas
114
125
115
126
uri = "<Atlas connection string>"
116
127
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))
118
129
119
130
Replica Set
120
131
~~~~~~~~~~~
121
132
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
+
122
137
.. tabs::
123
138
124
139
.. tab:: Synchronous
@@ -140,6 +155,9 @@ Replica Set
140
155
Network Compression
141
156
-------------------
142
157
158
+ The following sections describe how to connect to MongoDB while specifying network
159
+ compression algorithms.
160
+
143
161
Compression Algorithms
144
162
~~~~~~~~~~~~~~~~~~~~~~
145
163
@@ -151,6 +169,8 @@ To learn more about specifying compression algorithms, see
151
169
zlib Compression Level
152
170
~~~~~~~~~~~~~~~~~~~~~~
153
171
172
+ The following tabs demonstrate how to specify a compression level for the ``zlib`` compressor:
173
+
154
174
.. tabs::
155
175
156
176
.. tab:: MongoClient
@@ -159,8 +179,8 @@ zlib Compression Level
159
179
.. code-block:: python
160
180
161
181
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>)
164
184
165
185
.. tab:: Connection String
166
186
:tabid: connectionstring
@@ -178,17 +198,17 @@ zlib Compression Level
178
198
.. code-block:: python
179
199
180
200
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>)
183
203
184
204
.. tab:: Connection String (Asynchronous)
185
205
:tabid: connectionstring-async
186
206
187
207
.. code-block:: python
188
208
189
209
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
190
- "compressors=zlib"
191
- "zlibCompressionLevel=<zlib compression level>")
210
+ "compressors=zlib"
211
+ "zlibCompressionLevel=<zlib compression level>")
192
212
client = pymongo.AsyncMongoClient(uri)
193
213
194
214
To learn more about setting the zlib compression level, see
@@ -197,6 +217,10 @@ To learn more about setting the zlib compression level, see
197
217
Server Selection
198
218
----------------
199
219
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
+
200
224
.. tabs::
201
225
202
226
.. tab:: Synchronous
@@ -205,22 +229,26 @@ Server Selection
205
229
.. code-block:: python
206
230
207
231
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
208
- server_selector=<selector function>)
232
+ server_selector=<selector function>)
209
233
210
234
.. tab:: Asynchronous
211
235
:tabid: async
212
236
213
237
.. code-block:: python
214
238
215
239
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
216
- server_selector=<selector function>)
240
+ server_selector=<selector function>)
217
241
218
242
To learn more about customizing server selection, see
219
243
:ref:`pymongo-server-selection`.
220
244
221
245
{+stable-api+}
222
246
--------------
223
247
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
+
224
252
.. tabs::
225
253
226
254
.. tab:: Synchronous
@@ -231,7 +259,7 @@ To learn more about customizing server selection, see
231
259
from pymongo.server_api import ServerApi
232
260
233
261
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>",
234
- server_api=ServerApi("<{+stable-api+} version>"))
262
+ server_api=ServerApi("<{+stable-api+} version>"))
235
263
236
264
.. tab:: Asynchronous
237
265
:tabid: async
@@ -241,7 +269,7 @@ To learn more about customizing server selection, see
241
269
from pymongo.server_api import ServerApi
242
270
243
271
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>",
244
- server_api=ServerApi("<{+stable-api+} version>"))
272
+ server_api=ServerApi("<{+stable-api+} version>"))
245
273
246
274
To learn more about the {+stable-api+}, see :ref:`pymongo-stable-api`.
247
275
@@ -251,6 +279,8 @@ Limit Server Execution Time
251
279
timeout Block
252
280
~~~~~~~~~~~~~
253
281
282
+ The following code shows how to set a client-side timeout by using the ``timeout()`` method:
283
+
254
284
.. code-block:: python
255
285
256
286
with pymongo.timeout(<timeout length>):
@@ -261,6 +291,9 @@ To learn more about client-side timeouts, see :ref:`pymongo-csot`.
261
291
timeoutMS Connection Option
262
292
~~~~~~~~~~~~~~~~~~~~~~~~~~~
263
293
294
+ The following tabs demonstrate how to set a client-side timeout by using the ``timeoutMS``
295
+ connection option:
296
+
264
297
.. tabs::
265
298
266
299
.. tab:: MongoClient
@@ -269,7 +302,7 @@ timeoutMS Connection Option
269
302
.. code-block:: python
270
303
271
304
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname@:<port>",
272
- timeoutMS=<timeout length>)
305
+ timeoutMS=<timeout length>)
273
306
274
307
.. tab:: Connection String
275
308
:tabid: connectionstring
@@ -285,7 +318,7 @@ timeoutMS Connection Option
285
318
.. code-block:: python
286
319
287
320
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname@:<port>",
288
- timeoutMS=<timeout length>)
321
+ timeoutMS=<timeout length>)
289
322
290
323
.. tab:: Connection String (Asynchronous)
291
324
:tabid: connectionstring-async
0 commit comments