Skip to content

Commit aace45f

Browse files
committed
Revert "DOCSP-37496 - FAQ (#35)"
This reverts commit 8fba8fe.
1 parent 8fba8fe commit aace45f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3500
-2171
lines changed

snooty.toml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
name = "pymongo"
22
title = "PyMongo"
33
toc_landing_pages = [
4+
"/get-started",
5+
"/write-operations",
6+
"/read",
7+
"/connect"
48
]
59

610
intersphinx = [
@@ -12,13 +16,16 @@ sharedinclude_root = "https://raw.githubusercontent.com/10gen/docs-shared/main/"
1216

1317
[constants]
1418
driver-short = "PyMongo"
19+
driver-long = "PyMongo, the MongoDB synchronous Python driver,"
1520
language = "Python"
1621
mongo-community = "MongoDB Community Edition"
1722
mongo-enterprise = "MongoDB Enterprise Edition"
18-
docs-branch = "master" # always set this to the docs branch (i.e. master, 1.7, 1.8, etc.)
23+
docs-branch = "master" # always set this to the docs branch (i.e. master, 1.7, 1.8, etc.)
1924
version-number = "4.6"
20-
patch-version-number = "{+version-number+}.1" # always set this to the driver branch (i.e. 1.7.0, 1.8.0, etc.)
25+
patch-version-number = "{+version-number+}.1" # always set this to the driver branch (i.e. 1.7.0, 1.8.0, etc.)
2126
version = "v{+version-number+}"
2227
example = "https://raw.githubusercontent.com/mongodb/docs-pymongo/{+docs-branch+}/source/includes/code-examples"
2328
stable-api = "Stable API"
24-
api-root = "https://pymongo.readthedocs.io/en/{+patch-version-number+}/api/index.html"
29+
api-root = "https://pymongo.readthedocs.io/en/{+patch-version-number+}/api/"
30+
string-data-type = "``str``"
31+
int-data-type = "``int``"

source/connect.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.. _pymongo-connect:
2+
3+
==================
4+
Connect to MongoDB
5+
==================
6+
7+
.. meta::
8+
:description: Learn how to use (+driver-short+} to connect to from MongoDB.
9+
10+
.. toctree::
11+
:titlesonly:
12+
:maxdepth: 1
13+
14+
/connect/tls
15+
/connect/client-certificates
16+
17+
- :ref:`pymongo-tls`
18+
- :ref:`pymongo-client-certificates`

source/connect/connection-options.txt

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
.. _pymongo-connection-options:
2+
3+
==================
4+
Connection Options
5+
==================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:keywords: connection string, URI, server, Atlas, settings, configure
19+
20+
Overview
21+
--------
22+
23+
This section describes the MongoDB connection and authentication options
24+
available in {+driver-short+}. You can configure your connection using either
25+
the connection URI or arguments to the ``MongoClient`` constructor.
26+
27+
.. _pymongo-connection-uri:
28+
29+
Using the Connection URI
30+
~~~~~~~~~~~~~~~~~~~~~~~~
31+
32+
If you pass a connection URI to the ``MongoClient`` constructor, you can include
33+
connection options in the string as ``<name>=<value>`` pairs. In the following example,
34+
the connection URI contains the ``connect_timeout`` option with a value of ``60000``
35+
and the ``tls`` option with a value of ``true``:
36+
37+
.. literalinclude:: /includes/connect/local_connection_config.py
38+
:language: python
39+
:dedent:
40+
:start-after: # start local connection config
41+
:end-before: # end local connection config
42+
43+
.. _pymongo-mongo-client-settings:
44+
45+
Using a ``MongoClient``
46+
~~~~~~~~~~~~~~~~~~~~~~~
47+
48+
You can pass connection options as arguments to the ``MongoClient`` constructor
49+
instead of including them in your connection URI.
50+
Configuring the connection this way makes it easier to
51+
change settings at runtime and helps you catch errors during compilation.
52+
The following example shows how to use the ``MongoClient`` constructor to set
53+
connection options:
54+
55+
.. literalinclude:: /includes/connect/mongoclient_settings_config.py
56+
:language: python
57+
:dedent:
58+
:start-after: # start mongo client settings config
59+
:end-before: # end mongo client settings config
60+
61+
Connection Options
62+
------------------
63+
64+
The following sections describe the connection options available in {+driver-short+}.
65+
If a ``MongoClient`` parameter maps to more than one
66+
option in the connection string, the **Connection URI Example** shows all
67+
relevant options.
68+
69+
.. todo: .. note::
70+
If you're using a query parameter for a time duration, the value must be in
71+
milliseconds. For example, to specify 60 seconds, use the value ``60000``. If you're
72+
using a ``MongoClientSettings`` object for a time duration, use the appropriate
73+
``TimeSpan`` value.
74+
75+
Network Compression
76+
~~~~~~~~~~~~~~~~~~~
77+
78+
.. list-table::
79+
:header-rows: 1
80+
:widths: 30 70
81+
82+
* - Connection Option
83+
- Description
84+
85+
* - **compressors**
86+
- | The preferred compression types, in order, for wire-protocol messages sent to
87+
| or received from the server. The driver uses the first of these compression types
88+
| that the server supports.
89+
|
90+
| **Data Type**: {+string-data-type+}
91+
| **Default**: ``None``
92+
| **MongoClient Example**: ``compressors = "snappy,zstd,zlib"``
93+
| **Connection URI Example**: ``compressors=snappy,zstd,zlib``
94+
95+
* - **zlibCompressionLevel**
96+
- | The compression level for zlib to use. This option accepts
97+
| an integer value between ``-1`` and ``9``:
98+
|
99+
| - **-1:** (Default). zlib uses its default compression level (usually ``6``).
100+
| - **0:** No compression.
101+
| - **1:** Fastest speed but lowest compression.
102+
| - **9:** Best compression but slowest speed.
103+
|
104+
| **Data Type**: {+int-data-type+}
105+
| **Default**: ``-1``
106+
| **MongoClient Example**: ``zlibCompressionLevel = 3``
107+
| **Connection URI Example**: ``zlibCompressionLevel=3``
108+
109+
Server Selection
110+
~~~~~~~~~~~~~~~~
111+
112+
.. list-table::
113+
:header-rows: 1
114+
:widths: 30 70
115+
116+
* - Connection Option
117+
- Description
118+
119+
* - **server_selector**
120+
- | A user-defined Python function called by {+driver-short+} to choose the server
121+
| to run an operation against. For more information, see
122+
| :ref:`<pymongo-server-selection>`.
123+
|
124+
| **Data Type**: ``callable``
125+
| **Default**: ``None``
126+
| **MongoClient Example**: ``server_selector = your_function``
127+
| **Connection URI Example**: N/A
128+
129+
.. _secondary-reads:
130+
131+
Secondary Reads
132+
---------------
133+
134+
By default, an instance of ``MongoClient`` sends queries to
135+
the primary member of the replica set. To use secondary members for queries instead, change
136+
the read preference as shown in the following example:
137+
138+
.. code-block:: python
139+
140+
>>> client = MongoClient(
141+
... 'localhost:27017',
142+
... replicaSet='foo',
143+
... readPreference='secondaryPreferred')
144+
>>> client.read_preference
145+
SecondaryPreferred(tag_sets=None)
146+
147+
Now the ``MongoClient`` sends all queries to the secondary members of the replica set. If there are
148+
no secondary members, the client uses the primary member as a fallback. If you have
149+
queries you would prefer to never send to the primary, you can specify that
150+
using the ``secondary`` read preference.
151+
152+
.. _health-monitoring:
153+
154+
Health Monitoring
155+
-----------------
156+
157+
When you initialize a ``MongoClient``, it launches background threads to
158+
monitor the replica set for the following changes:
159+
160+
- Health: Detect when a member goes down or comes up, or if a different member
161+
becomes primary.
162+
- Configuration: Detect when members are added or removed, and detect changes
163+
in members' tags.
164+
- Latency: Track a moving average of each member's ping time.
165+
166+
Replica-set monitoring ensures that queries are continually routed to the proper
167+
members as the state of the replica set changes.
168+
169+
.. _mongos-load-balancing:
170+
171+
``mongos`` Load Balancing
172+
-------------------------
173+
174+
You can configure an instance of ``~pymongo.mongo_client.MongoClient``
175+
with a list of ``mongos`` server addresses:
176+
177+
.. code-block:: python
178+
179+
>> client = MongoClient('mongodb://host1,host2,host3')
180+
181+
Each member of the list must be a single ``mongos`` server. Multi-homed and round
182+
robin DNS addresses are **not** supported. The client continuously
183+
monitors the availability of all ``mongos`` servers. It also monitors its
184+
network latency to each server.
185+
186+
PyMongo distributes operations evenly among the set of ``mongos`` servers within its
187+
``localThresholdMS`` (similar to how it distributes reads to secondaries
188+
in a replica set). By default, the threshold is 15 ms.
189+
190+
The server with the lowest latency, and all servers with latencies no more than
191+
``localThresholdMS`` beyond the server with the lowest latency, receive
192+
operations equally. For example, consider the following three ``mongos`` servers:
193+
194+
- host1: 20 ms
195+
- host2: 35 ms
196+
- host3: 40 ms
197+
198+
By default, the ``localThresholdMS`` is 15 ms, so PyMongo uses "host1" and "host2"
199+
evenly. It uses "host1" because its network latency to the driver is shortest. It
200+
uses "host2" because its latency is within 15 ms of the server with the lowest latency.
201+
PyMongo doesn't use "host3" because its latency is 20 ms beyond the server with the
202+
lowest latency.
203+
204+
To ensure that all servers are within the threshold, set ``localThresholdMS`` to 30
205+
ms as shown in the following example:
206+
207+
.. code-block:: python
208+
209+
>> client = MongoClient('mongodb://host1,host2,host3/?localThresholdMS=30')
210+
211+
.. warning::
212+
213+
Do **not** connect PyMongo to a pool of ``mongos`` instances through a
214+
load balancer. A single socket connection must always route to the same
215+
``mongos`` instance for proper cursor support.
File renamed without changes.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
.. _pymongo-network-compression:
2+
3+
========================
4+
Compress Network Traffic
5+
========================
6+
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+
17+
.. meta::
18+
:keywords: zlib, zstandard, zstd, snappy
19+
20+
Overview
21+
--------
22+
23+
{+driver-short+} provides a connection option to compress messages, which reduces the amount
24+
of data passed over the network between MongoDB and your application.
25+
26+
{+driver-short+} supports the following compression algorithms.
27+
28+
1. `Snappy <https://google.github.io/snappy/>`__: Available in MongoDB 3.6 and later. This
29+
option requires the `python-snappy <https://pypi.org/project/python-snappy/>`__ package.
30+
31+
2. `Zlib <https://zlib.net/>`__: Available in MongoDB 3.6 and later. This option requires
32+
the zlib module, included in the standard library in Python v1.5 and later.
33+
34+
3. `Zstandard <https://github.com/facebook/zstd/>`__: Available in MongoDB 4.2 and later.
35+
This option requires the `zstandard <https://pypi.org/project/zstandard/>`__ package.
36+
37+
If you don't specify a compression algorithm, {+driver-short+} doesn't compress your
38+
network traffic. If you specify multiple compression algorithms, the driver selects the
39+
first one in the list supported by your MongoDB instance.
40+
41+
.. _pymongo-enable-compression:
42+
43+
Specify Compression Algorithms
44+
------------------------------
45+
46+
To enable compression for the connection to your MongoDB instance, use the
47+
``compressors`` connection option and specify the compression algorithms you want to use.
48+
You can do this in two ways:
49+
50+
- Pass the algorithms as an argument to the ``MongoClient`` constructor.
51+
- Specify the algorithms in your connection string.
52+
53+
The following code example shows both options:
54+
55+
.. tabs::
56+
57+
.. tab:: MongoClient
58+
:tabid: mongoclient
59+
60+
.. code-block:: python
61+
:emphasize-lines: 2
62+
63+
client = pymongo.MongoClient("mongodb://<username>:<password>@<hostname@:<port>",
64+
compressors = "snappy,zstd,zlib")
65+
66+
.. tab:: Connection String
67+
:tabid: connectionstring
68+
69+
.. code-block:: python
70+
:emphasize-lines: 2
71+
72+
uri = ("mongodb://<username>:<password>@<hostname>:<port>/?"
73+
"compressors=snappy,zstd,zlib")
74+
client = pymongo.MongoClient(uri)
75+
76+
Set the zlib Compression Level
77+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
78+
79+
If you specify ``zlib`` as one of your compression algorithms, you can also use the
80+
``zlibCompressionLevel`` option to specify a compression level. This option accepts
81+
an integer value between ``-1`` and ``9``:
82+
83+
- **-1:** (Default). zlib uses its default compression level (usually ``6``).
84+
- **0:** No compression.
85+
- **1:** Fastest speed but lowest compression.
86+
- **9:** Best compression but slowest speed.
87+
88+
The following code example specifies the ``zlib`` compression algorithm and a value of
89+
``1`` for the ``zlibCompressionLevel`` option:
90+
91+
.. tabs::
92+
93+
.. tab:: MongoClient
94+
:tabid: mongoclient
95+
96+
.. code-block:: python
97+
:emphasize-lines: 2-3
98+
99+
client = pymongo.MongoClient("mongodb://<username>:<password>@<hostname@:<port>",
100+
compressors = "zlib",
101+
zlibCompressionLevel=1)
102+
103+
.. tab:: Connection String
104+
:tabid: connectionstring
105+
106+
.. code-block:: python
107+
:emphasize-lines: 2-3
108+
109+
uri = ("mongodb://<username>:<password>@<hostname>:<port>/?"
110+
"compressors=zlib"
111+
"zlibCompressionLevel=1")
112+
client = pymongo.MongoClient(uri)

0 commit comments

Comments
 (0)