forked from mongodb/docs-pymongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection-options.txt
122 lines (86 loc) · 3.7 KB
/
connection-options.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
.. _pymongo-connection-options:
==========================
Specify Connection Options
==========================
.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol
.. facet::
:name: genre
:values: reference
.. meta::
:keywords: connection string, URI, server, Atlas, settings, configure
.. toctree::
:titlesonly:
:maxdepth: 1
Compress Network Traffic </connect/connection-options/network-compression>
Customize Server Selection </connect/connection-options/server-selection>
Stable API </connect/connection-options/stable-api>
Limit Server Execution Time </connect/connection-options/csot>
Connection Pools </connect/connection-options/connection-pools>
Overview
--------
This section describes the MongoDB connection and authentication options
available in {+driver-short+}. You can configure your connection by using either
the connection URI or arguments to the ``MongoClient`` constructor.
.. _pymongo-connection-uri:
Using the Connection URI
~~~~~~~~~~~~~~~~~~~~~~~~
If you pass a connection URI to the ``MongoClient`` constructor, you can include
connection options in the string as ``<name>=<value>`` pairs. In the following example,
the connection URI contains the ``connectTimeoutMS`` option with a value of ``60000``
and the ``tls`` option with a value of ``true``. Select the :guilabel:`Synchronous` or
:guilabel:`Asynchronous` tab to see the corresponding code:
.. tabs::
.. tab:: Synchronous
:tabid: sync
.. code-block:: python
uri = "mongodb://<hostname>:<port>/?connectTimeoutMS=60000&tls=true"
client = pymongo.MongoClient(uri)
.. tab:: Asynchronous
:tabid: async
.. code-block:: python
uri = "mongodb://<hostname>:<port>/?connectTimeoutMS=60000&tls=true"
client = pymongo.AsyncMongoClient(uri)
.. _pymongo-mongo-client-settings:
Using a ``MongoClient``
~~~~~~~~~~~~~~~~~~~~~~~
You can pass connection options as arguments to the ``MongoClient`` constructor
instead of including them in your connection URI.
Configuring the connection this way makes it easier to
change settings at runtime and helps you catch errors during compilation.
The following example shows how to use the ``MongoClient`` constructor to set
connection options. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to
see the corresponding code:
.. tabs::
.. tab:: Synchronous
:tabid: sync
.. code-block:: python
uri = "mongodb://<hostname>:<port>"
client = pymongo.MongoClient(uri, connectTimeoutMS=60000, tls=True)
.. tab:: Asynchronous
:tabid: async
.. code-block:: python
uri = "mongodb://<hostname>:<port>"
client = pymongo.AsyncMongoClient(uri, connectTimeoutMS=60000, tls=True)
Connection Options
------------------
To learn about the connection options available in {+driver-short+}, see the following
sections:
- :ref:`Enable Authentication <pymongo-security>`
- :ref:`Compress Network Traffic <pymongo-network-compression>`
- :ref:`Customize Server Selection <pymongo-server-selection>`
- :ref:`Stable API <pymongo-stable-api>`
- :ref:`Limit Server Execution Time <pymongo-csot>`
- :ref:`Connection Pools <pymongo-connection-pools>`
- :ref:`Configure CRUD Operations <pymongo-configure-crud>`
.. tip:: Authentication and Encryption
To learn how to enable TLS encryption and authentication in {+driver-short+},
see :ref:`pymongo-tls` and :ref:`pymongo-auth` in the Security section.
API Documentation
-----------------
To learn more about creating a ``MongoClient`` object in {+driver-short+},
see the following API documentation:
- `MongoClient <{+api-root+}pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`__