@@ -46,39 +46,95 @@ 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
58
75
Local Deployment
59
76
~~~~~~~~~~~~~~~~
60
77
61
- .. code-block:: python
78
+ .. tabs::
62
79
63
- uri = "mongodb://localhost:27017/"
64
- client = MongoClient(uri)
80
+ .. tab:: Synchronous
81
+ :tabid: sync
82
+
83
+ .. code-block:: python
84
+
85
+ client = MongoClient("mongodb://localhost:27017/")
86
+
87
+ .. tab:: Asynchronous
88
+ :tabid: async
89
+
90
+ .. code-block:: python
91
+
92
+ uri = "mongodb://localhost:27017/"
93
+ client = AsyncMongoClient(uri)
65
94
66
95
Atlas
67
96
~~~~~
68
97
69
- .. code-block:: python
98
+ .. tabs::
70
99
71
- uri = "<Atlas connection string>"
72
- client = MongoClient(uri, server_api=pymongo.server_api.ServerApi(
73
- version="1", strict=True, deprecation_errors=True))
100
+ .. tab:: Synchronous
101
+ :tabid: sync
102
+
103
+ .. code-block:: python
104
+
105
+ uri = "<Atlas connection string>"
106
+ client = MongoClient(uri, server_api=pymongo.server_api.ServerApi(
107
+ version="1", strict=True, deprecation_errors=True))
108
+
109
+ .. tab:: Asynchronous
110
+ :tabid: async
111
+
112
+ .. code-block:: python
113
+
114
+ uri = "<Atlas connection string>"
115
+ client = AsyncMongoClient(uri, server_api=pymongo.server_api.ServerApi(
116
+ version="1", strict=True, deprecation_errors=True))
74
117
75
118
Replica Set
76
119
~~~~~~~~~~~
77
120
78
- .. code-block:: python
121
+ .. tabs::
122
+
123
+ .. tab:: Synchronous
124
+ :tabid: sync
125
+
126
+ .. code-block:: python
127
+
128
+ uri = "mongodb://<replica set member>:<port>/?replicaSet=<replica set name>"
129
+ client = MongoClient(uri)
79
130
80
- uri = "mongodb://<replica set member>:<port>/?replicaSet=<replica set name>"
81
- client = MongoClient(uri)
131
+ .. tab:: Asynchronous
132
+ :tabid: async
133
+
134
+ .. code-block:: python
135
+
136
+ uri = "mongodb://<replica set member>:<port>/?replicaSet=<replica set name>"
137
+ client = AsyncMongoClient(uri)
82
138
83
139
Network Compression
84
140
-------------------
@@ -115,29 +171,76 @@ zlib Compression Level
115
171
"zlibCompressionLevel=<zlib compression level>")
116
172
client = pymongo.MongoClient(uri)
117
173
174
+ .. tab:: MongoClient (Asynchronous)
175
+ :tabid: mongoclient-async
176
+
177
+ .. code-block:: python
178
+
179
+ client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
180
+ compressors = "zlib",
181
+ zlibCompressionLevel=<zlib compression level>)
182
+
183
+ .. tab:: Connection String (Asynchronous)
184
+ :tabid: connectionstring-async
185
+
186
+ .. code-block:: python
187
+
188
+ uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
189
+ "compressors=zlib"
190
+ "zlibCompressionLevel=<zlib compression level>")
191
+ client = pymongo.AsyncMongoClient(uri)
192
+
118
193
To learn more about setting the zlib compression level, see
119
194
:ref:`pymongo-enable-compression` in the Network Compression guide.
120
195
121
196
Server Selection
122
197
----------------
123
198
124
- .. code-block:: python
199
+ .. tabs::
125
200
126
- client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
127
- server_selector=<selector function>)
201
+ .. tab:: Synchronous
202
+ :tabid: sync
203
+
204
+ .. code-block:: python
205
+
206
+ client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
207
+ server_selector=<selector function>)
208
+
209
+ .. tab:: Asynchronous
210
+ :tabid: async
211
+
212
+ .. code-block:: python
213
+
214
+ client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
215
+ server_selector=<selector function>)
128
216
129
217
To learn more about customizing server selection, see
130
218
:ref:`pymongo-server-selection`.
131
219
132
220
{+stable-api+}
133
221
--------------
134
222
135
- .. code-block:: python
223
+ .. tabs::
136
224
137
- from pymongo.server_api import ServerApi
225
+ .. tab:: Synchronous
226
+ :tabid: sync
138
227
139
- client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>",
140
- server_api=ServerApi("<{+stable-api+} version>"))
228
+ .. code-block:: python
229
+
230
+ from pymongo.server_api import ServerApi
231
+
232
+ client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>",
233
+ server_api=ServerApi("<{+stable-api+} version>"))
234
+
235
+ .. tab:: Asynchronous
236
+ :tabid: async
237
+
238
+ .. code-block:: python
239
+
240
+ from pymongo.server_api import ServerApi
241
+
242
+ client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>",
243
+ server_api=ServerApi("<{+stable-api+} version>"))
141
244
142
245
To learn more about the {+stable-api+}, see :ref:`pymongo-stable-api`.
143
246
@@ -175,5 +278,20 @@ timeoutMS Connection Option
175
278
uri = "mongodb://<db_username>:<db_password>@<hostname:<port>/?timeoutMS=<timeout length>"
176
279
client = pymongo.MongoClient(uri)
177
280
178
- To learn more about client-side timeouts, see :ref:`pymongo-csot`.
281
+ .. tab:: MongoClient (Asynchronous)
282
+ :tabid: mongoclient-async
179
283
284
+ .. code-block:: python
285
+
286
+ client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname@:<port>",
287
+ timeoutMS=<timeout length>)
288
+
289
+ .. tab:: Connection String (Asynchronous)
290
+ :tabid: connectionstring-async
291
+
292
+ .. code-block:: python
293
+
294
+ uri = "mongodb://<db_username>:<db_password>@<hostname:<port>/?timeoutMS=<timeout length>"
295
+ client = pymongo.AsyncMongoClient(uri)
296
+
297
+ To learn more about client-side timeouts, see :ref:`pymongo-csot`.
0 commit comments