44Connect to MongoDB
55==================
66
7+ .. contents:: On this page
8+ :local:
9+ :backlinks: none
10+ :depth: 2
11+ :class: singlecol
12+
13+ .. facet::
14+ :name: genre
15+ :values: reference
16+
717.. meta::
8- :description: Learn how to use (+driver-short+} to connect to from MongoDB.
18+ :description: Learn how to use {+driver-short+} to connect to MongoDB.
19+ :keywords: client, ssl
920
1021.. toctree::
1122 :titlesonly:
@@ -17,8 +28,202 @@ Connect to MongoDB
1728 /connect/stable-api
1829 /connect/csot
1930
20- - :ref:`pymongo-tls`
21- - :ref:`pymongo-network-compression`
22- - :ref:`pymongo-server-selection`
23- - :ref:`pymongo-stable-api`
24- - :ref:`pymongo-csot`
31+ Overview
32+ --------
33+
34+ This page contains code examples that show how to connect your Python application
35+ to MongoDB with various settings.
36+
37+ .. tip::
38+
39+ To learn more about the connection options on this page, see the link
40+ provided in each section.
41+
42+ To use a connection example from this page, copy the code example into the
43+ :ref:`sample application <pymongo-connect-sample>` or your own application.
44+ Be sure to replace all placeholders in the code examples, such as ``<hostname>``, with
45+ the relevant values for your MongoDB deployment.
46+
47+ .. _pymongo-connect-sample:
48+
49+ .. include:: /includes/usage-examples/sample-app-intro.rst
50+
51+ .. literalinclude:: /includes/usage-examples/connect-sample-app.py
52+ :language: python
53+ :copyable: true
54+ :linenos:
55+ :emphasize-lines: 5-7
56+
57+ Transport Layer Security (TLS)
58+ ------------------------------
59+
60+ Enable TLS
61+ ~~~~~~~~~~
62+
63+ .. include:: /includes/connect/tls-tabs.rst
64+
65+ To learn more about enabling TLS, see :ref:`pymongo-enable-tls` in
66+ the TLS configuration guide.
67+
68+ Specify a Certificate Authority (CA) File
69+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70+
71+ .. include:: /includes/connect/ca-file-tabs.rst
72+
73+ To learn more about specifying a CA file, see :ref:`pymongo-specify-ca-file` in
74+ the TLS configuration guide.
75+
76+ Disable OCSP Checks
77+ ~~~~~~~~~~~~~~~~~~~
78+
79+ .. include:: /includes/connect/ocsp-tabs.rst
80+
81+ To learn more about disabling OCSP checks, see :ref:`pymongo-disable-ocsp` in
82+ the TLS configuration guide.
83+
84+ Specify a Certificate Revocation List (CRL)
85+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
86+
87+ .. include:: /includes/connect/crl-tabs.rst
88+
89+ To learn more about specifying a CRL, see :ref:`pymongo-crl` in
90+ the TLS configuration guide.
91+
92+ Present a Client Certificate
93+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
94+
95+ .. include:: /includes/connect/client-cert-tabs.rst
96+
97+ To learn more about specifying a client certificate, see :ref:`pymongo-client-cert` in
98+ the TLS configuration guide.
99+
100+ Provide a Certificate Key File Password
101+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102+
103+ .. include:: /includes/connect/key-file-password.rst
104+
105+ To learn more about providing a key file password, see :ref:`pymongo-key-file-password` in
106+ the TLS configuration guide.
107+
108+ Allow Insecure TLS
109+ ~~~~~~~~~~~~~~~~~~
110+
111+ .. include:: /includes/connect/insecure-tls-tabs.rst
112+
113+ To learn more about allowing insecure TLS, see :ref:`pymongo-insecure-tls` in
114+ the TLS configuration guide.
115+
116+ Disable Certificate Validation
117+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
118+
119+ .. include:: /includes/connect/disable-cert-validation-tabs.rst
120+
121+ To learn more about disabling certificate validation, see :ref:`pymongo-insecure-tls` in
122+ the TLS configuration guide.
123+
124+ Disable Hostname Verification
125+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
126+
127+ .. include:: /includes/connect/disable-host-verification-tabs.rst
128+
129+ To learn more about disabling hostname verification, see :ref:`pymongo-insecure-tls` in
130+ the TLS configuration guide.
131+
132+ Network Compression
133+ -------------------
134+
135+ Compression Algorithms
136+ ~~~~~~~~~~~~~~~~~~~~~~
137+
138+ .. include:: /includes/connect/compression-tabs.rst
139+
140+ To learn more about specifying compression algorithms, see
141+ :ref:`pymongo-enable-compression` in the Network Compression guide.
142+
143+ zlib Compression Level
144+ ~~~~~~~~~~~~~~~~~~~~~~
145+
146+ .. tabs::
147+
148+ .. tab:: MongoClient
149+ :tabid: mongoclient
150+
151+ .. code-block:: python
152+
153+ client = pymongo.MongoClient("mongodb://<username>:<password>@<hostname>:<port>",
154+ compressors = "zlib",
155+ zlibCompressionLevel=<zlib compression level>)
156+
157+ .. tab:: Connection String
158+ :tabid: connectionstring
159+
160+ .. code-block:: python
161+
162+ uri = ("mongodb://<username>:<password>@<hostname>:<port>/?"
163+ "compressors=zlib"
164+ "zlibCompressionLevel=<zlib compression level>")
165+ client = pymongo.MongoClient(uri)
166+
167+ To learn more about setting the zlib compression level, see
168+ :ref:`pymongo-enable-compression` in the Network Compression guide.
169+
170+ Server Selection
171+ ----------------
172+
173+ .. code-block:: python
174+
175+ client = pymongo.MongoClient("mongodb://<username>:<password>@<hostname>:<port>",
176+ server_selector=<selector function>)
177+
178+ To learn more about customizing server selection, see
179+ :ref:`pymongo-server-selection`.
180+
181+ {+stable-api+}
182+ --------------
183+
184+ .. code-block:: python
185+ :emphasize-lines: 4
186+
187+ from pymongo.server_api import ServerApi
188+
189+ client = pymongo.MongoClient("mongodb://<username>:<password>@<hostname:<port>",
190+ server_api=ServerApi("<{+stable-api+} version>"))
191+
192+ To learn more about the {+stable-api+}, see :ref:`pymongo-stable-api`.
193+
194+ Limit Server Execution Time
195+ ---------------------------
196+
197+ timeout Block
198+ ~~~~~~~~~~~~~
199+
200+ .. code-block:: python
201+
202+ with pymongo.timeout(<timeout length>):
203+ # perform operations here
204+
205+ To learn more about client-side timeouts, see :ref:`pymongo-csot`.
206+
207+ timeoutMS Connection Option
208+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
209+
210+ .. tabs::
211+
212+ .. tab:: MongoClient
213+ :tabid: mongoclient
214+
215+ .. code-block:: python
216+
217+ client = pymongo.MongoClient("mongodb://<username>:<password>@<hostname@:<port>",
218+ timeoutMS=<timeout length>)
219+
220+ .. tab:: Connection String
221+ :tabid: connectionstring
222+
223+ .. code-block:: python
224+
225+ uri = "mongodb://<username>:<password>@<hostname:<port>/?timeoutMS=<timeout length>"
226+ client = pymongo.MongoClient(uri)
227+
228+ To learn more about client-side timeouts, see :ref:`pymongo-csot`.
229+
0 commit comments