@@ -46,43 +46,118 @@ the relevant values for your MongoDB deployment.
46
46
47
47
.. include:: /includes/usage-examples/sample-app-intro.rst
48
48
49
- .. literalinclude:: /includes/usage-examples/connect-sample-app.py
50
- :language: python
51
- :copyable: true
52
- :linenos:
53
- :emphasize-lines: 4-6
49
+ Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding
50
+ code:
51
+
52
+ .. tabs::
53
+
54
+ .. tab:: Synchronous
55
+ :tabid: sync
56
+
57
+ .. literalinclude:: /includes/usage-examples/connect-sample-app.py
58
+ :language: python
59
+ :copyable: true
60
+ :linenos:
61
+ :emphasize-lines: 4-6
62
+
63
+ .. tab:: Asynchronous
64
+ :tabid: async
65
+
66
+ .. literalinclude:: /includes/usage-examples/connect-sample-app-async.py
67
+ :language: python
68
+ :copyable: true
69
+ :linenos:
70
+ :emphasize-lines: 6-8
54
71
55
72
Connection
56
73
----------
57
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
+
58
78
Local Deployment
59
79
~~~~~~~~~~~~~~~~
60
80
61
- .. code-block:: python
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:
62
84
63
- uri = "mongodb://localhost:27017/"
64
- client = MongoClient(uri)
85
+ .. tabs::
86
+
87
+ .. tab:: Synchronous
88
+ :tabid: sync
89
+
90
+ .. code-block:: python
91
+
92
+ uri = "mongodb://localhost:27017/"
93
+ client = MongoClient(uri)
94
+
95
+ .. tab:: Asynchronous
96
+ :tabid: async
97
+
98
+ .. code-block:: python
99
+
100
+ uri = "mongodb://localhost:27017/"
101
+ client = AsyncMongoClient(uri)
65
102
66
103
Atlas
67
104
~~~~~
68
105
69
- .. code-block:: python
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:
70
109
71
- uri = "<Atlas connection string>"
72
- client = MongoClient(uri, server_api=pymongo.server_api.ServerApi(
73
- version="1", strict=True, deprecation_errors=True))
110
+ .. tabs::
111
+
112
+ .. tab:: Synchronous
113
+ :tabid: sync
114
+
115
+ .. code-block:: python
116
+
117
+ uri = "<Atlas connection string>"
118
+ client = MongoClient(uri, server_api=pymongo.server_api.ServerApi(
119
+ version="1", strict=True, deprecation_errors=True))
120
+
121
+ .. tab:: Asynchronous
122
+ :tabid: async
123
+
124
+ .. code-block:: python
125
+
126
+ uri = "<Atlas connection string>"
127
+ client = AsyncMongoClient(uri, server_api=pymongo.server_api.ServerApi(
128
+ version="1", strict=True, deprecation_errors=True))
74
129
75
130
Replica Set
76
131
~~~~~~~~~~~
77
132
78
- .. code-block:: python
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
+
137
+ .. tabs::
138
+
139
+ .. tab:: Synchronous
140
+ :tabid: sync
141
+
142
+ .. code-block:: python
143
+
144
+ uri = "mongodb://<replica set member>:<port>/?replicaSet=<replica set name>"
145
+ client = MongoClient(uri)
79
146
80
- uri = "mongodb://<replica set member>:<port>/?replicaSet=<replica set name>"
81
- client = MongoClient(uri)
147
+ .. tab:: Asynchronous
148
+ :tabid: async
149
+
150
+ .. code-block:: python
151
+
152
+ uri = "mongodb://<replica set member>:<port>/?replicaSet=<replica set name>"
153
+ client = AsyncMongoClient(uri)
82
154
83
155
Network Compression
84
156
-------------------
85
157
158
+ The following sections describe how to connect to MongoDB while specifying network
159
+ compression algorithms.
160
+
86
161
Compression Algorithms
87
162
~~~~~~~~~~~~~~~~~~~~~~
88
163
@@ -94,6 +169,8 @@ To learn more about specifying compression algorithms, see
94
169
zlib Compression Level
95
170
~~~~~~~~~~~~~~~~~~~~~~
96
171
172
+ The following tabs demonstrate how to specify a compression level for the ``zlib`` compressor:
173
+
97
174
.. tabs::
98
175
99
176
.. tab:: MongoClient
@@ -102,8 +179,8 @@ zlib Compression Level
102
179
.. code-block:: python
103
180
104
181
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
105
- compressors = "zlib",
106
- zlibCompressionLevel=<zlib compression level>)
182
+ compressors = "zlib",
183
+ zlibCompressionLevel=<zlib compression level>)
107
184
108
185
.. tab:: Connection String
109
186
:tabid: connectionstring
@@ -115,29 +192,84 @@ zlib Compression Level
115
192
"zlibCompressionLevel=<zlib compression level>")
116
193
client = pymongo.MongoClient(uri)
117
194
195
+ .. tab:: MongoClient (Asynchronous)
196
+ :tabid: mongoclient-async
197
+
198
+ .. code-block:: python
199
+
200
+ client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
201
+ compressors = "zlib",
202
+ zlibCompressionLevel=<zlib compression level>)
203
+
204
+ .. tab:: Connection String (Asynchronous)
205
+ :tabid: connectionstring-async
206
+
207
+ .. code-block:: python
208
+
209
+ uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
210
+ "compressors=zlib"
211
+ "zlibCompressionLevel=<zlib compression level>")
212
+ client = pymongo.AsyncMongoClient(uri)
213
+
118
214
To learn more about setting the zlib compression level, see
119
215
:ref:`pymongo-enable-compression` in the Network Compression guide.
120
216
121
217
Server Selection
122
218
----------------
123
219
124
- .. code-block:: python
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:
125
223
126
- client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
127
- server_selector=<selector function>)
224
+ .. tabs::
225
+
226
+ .. tab:: Synchronous
227
+ :tabid: sync
228
+
229
+ .. code-block:: python
230
+
231
+ client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
232
+ server_selector=<selector function>)
233
+
234
+ .. tab:: Asynchronous
235
+ :tabid: async
236
+
237
+ .. code-block:: python
238
+
239
+ client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
240
+ server_selector=<selector function>)
128
241
129
242
To learn more about customizing server selection, see
130
243
:ref:`pymongo-server-selection`.
131
244
132
245
{+stable-api+}
133
246
--------------
134
247
135
- .. code-block:: python
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:
136
251
137
- from pymongo.server_api import ServerApi
252
+ .. tabs::
138
253
139
- client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>",
140
- server_api=ServerApi("<{+stable-api+} version>"))
254
+ .. tab:: Synchronous
255
+ :tabid: sync
256
+
257
+ .. code-block:: python
258
+
259
+ from pymongo.server_api import ServerApi
260
+
261
+ client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>",
262
+ server_api=ServerApi("<{+stable-api+} version>"))
263
+
264
+ .. tab:: Asynchronous
265
+ :tabid: async
266
+
267
+ .. code-block:: python
268
+
269
+ from pymongo.server_api import ServerApi
270
+
271
+ client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>",
272
+ server_api=ServerApi("<{+stable-api+} version>"))
141
273
142
274
To learn more about the {+stable-api+}, see :ref:`pymongo-stable-api`.
143
275
@@ -147,6 +279,8 @@ Limit Server Execution Time
147
279
timeout Block
148
280
~~~~~~~~~~~~~
149
281
282
+ The following code shows how to set a client-side timeout by using the ``timeout()`` method:
283
+
150
284
.. code-block:: python
151
285
152
286
with pymongo.timeout(<timeout length>):
@@ -157,6 +291,9 @@ To learn more about client-side timeouts, see :ref:`pymongo-csot`.
157
291
timeoutMS Connection Option
158
292
~~~~~~~~~~~~~~~~~~~~~~~~~~~
159
293
294
+ The following tabs demonstrate how to set a client-side timeout by using the ``timeoutMS``
295
+ connection option:
296
+
160
297
.. tabs::
161
298
162
299
.. tab:: MongoClient
@@ -165,7 +302,7 @@ timeoutMS Connection Option
165
302
.. code-block:: python
166
303
167
304
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname@:<port>",
168
- timeoutMS=<timeout length>)
305
+ timeoutMS=<timeout length>)
169
306
170
307
.. tab:: Connection String
171
308
:tabid: connectionstring
@@ -175,5 +312,20 @@ timeoutMS Connection Option
175
312
uri = "mongodb://<db_username>:<db_password>@<hostname:<port>/?timeoutMS=<timeout length>"
176
313
client = pymongo.MongoClient(uri)
177
314
178
- To learn more about client-side timeouts, see :ref:`pymongo-csot`.
315
+ .. tab:: MongoClient (Asynchronous)
316
+ :tabid: mongoclient-async
317
+
318
+ .. code-block:: python
319
+
320
+ client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname@:<port>",
321
+ timeoutMS=<timeout length>)
322
+
323
+ .. tab:: Connection String (Asynchronous)
324
+ :tabid: connectionstring-async
179
325
326
+ .. code-block:: python
327
+
328
+ uri = "mongodb://<db_username>:<db_password>@<hostname:<port>/?timeoutMS=<timeout length>"
329
+ client = pymongo.AsyncMongoClient(uri)
330
+
331
+ To learn more about client-side timeouts, see :ref:`pymongo-csot`.
0 commit comments