Skip to content

Commit 906a49d

Browse files
committed
Merge remote-tracking branch 'upstream/master' into docsp-46812-type-hints
2 parents 9dc395a + ce4843c commit 906a49d

16 files changed

+456
-30
lines changed

config/redirects

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
define: prefix docs/languages/python/pymongo-driver
22
define: base https://www.mongodb.com/${prefix}
3-
define: versions v4.0 v4.1 v4.2 v4.3 v4.4 v4.5 v4.6 v4.7 v4.8 v4.9 4.10 master
3+
define: versions v4.0 v4.1 v4.2 v4.3 v4.4 v4.5 v4.6 v4.7 v4.8 v4.9 4.10 4.11 master
44

55
symlink: current -> master
66

snooty.toml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,21 @@ mdb-server = "MongoDB Server"
3131
mongo-community = "MongoDB Community Edition"
3232
mongo-enterprise = "MongoDB Enterprise Edition"
3333
docs-branch = "master" # always set this to the docs branch (i.e. master, 1.7, 1.8, etc.)
34-
version-number = "4.10"
35-
patch-version-number = "{+version-number+}.1" # always set this to the driver branch (i.e. 1.7.0, 1.8.0, etc.)
34+
version-number = "4.11"
35+
patch-version-number = "{+version-number+}" # always set this to the driver branch (i.e. 1.7.0, 1.8.0, etc.)
3636
version = "v{+version-number+}"
3737
stable-api = "Stable API"
3838
api-root = "https://pymongo.readthedocs.io/en/{+patch-version-number+}/api/"
3939
string-data-type = "``str``"
4040
int-data-type = "``int``"
4141
bool-data-type = "``bool``"
42+
django-odm = "Django MongoDB Backend"
43+
django-docs = "https://www.mongodb.com/docs/languages/python/django-mongodb/current"
44+
45+
[[banners]]
46+
targets = ["index.txt"]
47+
variant = "tip"
48+
value = """
49+
{+django-odm+} is now in Public Preview! \
50+
To discover its features and learn more, see the `{+django-odm+} documentation <{+django-docs+}>`__.
51+
"""

source/connect.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Connect to MongoDB
3030
Customize Server Selection </connect/server-selection>
3131
Stable API </connect/stable-api>
3232
Limit Server Execution Time </connect/csot>
33+
Connection Pools </connect/connection-pools>
3334

3435
Overview
3536
--------

source/connect/connection-options.txt

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,28 @@ Read and Write Operations
258258
time plus this value, the server isn't eligible for selection.
259259
|
260260
| **Data Type**: `read_preferences <{+api-root+}pymongo/read_preferences.html#pymongo.read_preferences>`__
261-
| **Default**: ``{+int-data-type+}``
261+
| **Default**: {+int-data-type+}
262262
| **MongoClient Example**: ``localThresholdMS=35``
263263
| **Connection URI Example**: ``localThresholdMS=35``
264264

265-
For more information about the connection option in this section, see :ref:`pymongo-databases-collections`.
265+
* - **retryReads**
266+
- | Specifies whether the client retries supported read operations. For more
267+
information, see :manual:`Retryable Reads </core/retryable-reads/>` in the {+mdb-server+}
268+
manual.
269+
|
270+
| **Data Type**: {+bool-data-type+}
271+
| **Default**: ``True``
272+
| **MongoClient Example**: ``retryReads=False``
273+
| **Connection URI Example**: ``retryReads=false``
274+
275+
* - **retryWrites**
276+
- | Specifies whether the client retries supported write operations. For more
277+
information, see :manual:`Retryable Writes </core/retryable-writes/>` in the {+mdb-server+}
278+
manual.
279+
|
280+
| **Data Type**: {+bool-data-type+}
281+
| **Default**: ``True``
282+
| **MongoClient Example**: ``retryWrites=False``
283+
| **Connection URI Example**: ``retryWrites=false``
284+
285+
For more information about the connection options in this section, see :ref:`pymongo-databases-collections`.

source/connect/connection-pools.txt

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
.. _pymongo-connection-pools:
2+
3+
================
4+
Connection Pools
5+
================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. contents:: On this page
12+
:local:
13+
:backlinks: none
14+
:depth: 2
15+
:class: singlecol
16+
17+
Overview
18+
--------
19+
20+
In this guide, you can learn about how {+driver-short+} uses connection pools to manage
21+
connections to a MongoDB deployment and how you can configure connection pool settings
22+
in your application.
23+
24+
A connection pool is a cache of open database connections maintained by {+driver-short+}.
25+
When your application requests a connection to MongoDB, {+driver-short+} seamlessly
26+
gets a connection from the pool, performs operations, and returns the connection
27+
to the pool for reuse.
28+
29+
Connection pools help reduce application latency and the number of times new connections
30+
are created by {+driver-short+}.
31+
32+
Configuring Connection Pools
33+
----------------------------
34+
35+
You can specify the following connection pool settings in your ``MongoClient`` object or in
36+
your connection URI:
37+
38+
.. list-table::
39+
:widths: 30 70
40+
:header-rows: 1
41+
42+
* - Setting
43+
- Description
44+
45+
* - ``connectTimeoutMS``
46+
- | Sets the time that {+driver-short+} waits when connecting a new
47+
socket before timing out.
48+
| Defaults to ``20000``
49+
50+
* - ``maxConnecting``
51+
- | Sets the maximum number of connections that each pool can establish concurrently.
52+
If this limit is reached, further requests wait until a connection is established
53+
or another in-use connection is checked back into the pool.
54+
| Defaults to ``2``
55+
56+
* - ``maxIdleTimeMS``
57+
- | Sets the maximum time that a connection can remain idle in the pool.
58+
| Defaults to ``None`` (no limit)
59+
60+
* - ``maxPoolSize``
61+
- | Sets the maximum number of concurrent connections that the pool maintains.
62+
If the maximum pool size is reached, further requests wait until a connection
63+
becomes available.
64+
| Defaults to ``100``
65+
66+
* - ``minPoolSize``
67+
- | Sets the minimum number of concurrent connections that the pool maintains. If
68+
the number of open connections falls below this value due to network errors,
69+
{+driver-short+} attempts to create new connections to maintain this minimum.
70+
| Defaults to ``0``
71+
72+
* - ``socketTimeoutMS``
73+
- | Sets the length of time that {+driver-short+} waits for a response from the server
74+
before timing out.
75+
| Defaults to ``None`` (no timeout)
76+
77+
* - ``waitQueueTimeoutMS``
78+
- | Sets how long a thread waits for a connection to become available in the connection pool
79+
before timing out.
80+
| Defaults to ``None`` (no timeout)
81+
82+
The following code creates a client with a maximum connection pool size of ``50`` by using the
83+
``maxPoolSize`` parameter:
84+
85+
.. code-block:: python
86+
87+
client = MongoClient(host, port, maxPoolSize=50)
88+
89+
The following code creates a client with the same configuration as the preceding example,
90+
but uses a connection URI:
91+
92+
.. code-block:: python
93+
94+
client = MongoClient("mongodb://<host>:<port>/?maxPoolSize=50")
95+
96+
Additional Information
97+
----------------------
98+
99+
To learn more about connection pools, see :manual:`Connection Pool Overview </administration/connection-pool-overview/>`
100+
in the {+mdb-server+} manual.
101+
102+
API Documentation
103+
~~~~~~~~~~~~~~~~~
104+
105+
To learn more about any of the methods or types discussed in this
106+
guide, see the following API documentation:
107+
108+
- `MongoClient <{+api-root+}pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`__

source/connect/connection-targets.txt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Choose a Connection Target
99
:values: reference
1010

1111
.. meta::
12-
:keywords: connection string, URI, server, settings, client
12+
:keywords: connection string, URI, server, settings, client, load balancing
1313

1414
.. contents:: On this page
1515
:local:
@@ -74,6 +74,15 @@ Replica Sets
7474
To connect to a replica set, specify the hostnames (or IP addresses) and
7575
port numbers of the replica-set members in your connection string.
7676

77+
The following code shows how to use {+driver-short+} to connect to a replica set
78+
that contains three hosts:
79+
80+
.. code-block:: python
81+
82+
from pymongo import MongoClient
83+
84+
client = MongoClient("mongodb://host1:27017,host2:27017,host3:27017")
85+
7786
If you aren't able to provide a full list of hosts in the replica set, you can
7887
specify one or more of the hosts in the replica set and instruct {+driver-short+} to
7988
perform automatic discovery to find the others. To instruct the driver to perform
@@ -94,6 +103,11 @@ hosts, including ``host1``:
94103
uri = "mongodb://host1:27017/?replicaSet=sampleRS"
95104
client = MongoClient(uri)
96105

106+
{+driver-short+} evenly load balances operations across deployments that are reachable
107+
within the client's ``localThresholdMS`` value. To learn more about how {+driver-short+} load
108+
balances operations across multiple MongoDB deployments, see the
109+
:ref:`pymongo-server-selection` guide.
110+
97111
.. note::
98112

99113
The ``MongoClient`` constructor is *non-blocking*.

source/connect/network-compression.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,10 @@ The following code example specifies the ``zlib`` compression algorithm and a va
9191
"compressors=zlib"
9292
"zlibCompressionLevel=1")
9393
client = pymongo.MongoClient(uri)
94+
95+
API Documentation
96+
-----------------
97+
98+
To learn more about any of the methods or types discussed in this
99+
guide, see the `MongoClient <{+api-root+}pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`__
100+
API documentation.

source/data-formats.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Specialized Data Formats
2121
:titlesonly:
2222
:maxdepth: 1
2323

24+
BSON </data-formats/bson>
2425
Custom Types </data-formats/custom-types>
2526
Dates & Times </data-formats/dates-and-times>
2627
UUIDs </data-formats/uuid>
@@ -33,6 +34,7 @@ You can use several types of specialized data formats in your {+driver-short+}
3334
application. To learn how to work with these data formats, see the following
3435
sections:
3536

37+
- Learn how to work with BSON documents in the :ref:`pymongo-bson` guide.
3638
- Learn how to encode and decode custom types in the :ref:`pymongo-custom-types` guide.
3739
- Learn how to work with Python ``datetime`` objects in {+driver-short+} in the
3840
:ref:`pymongo-dates-times` guide.

source/data-formats/bson.txt

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
.. _pymongo-bson:
2+
3+
====
4+
BSON
5+
====
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 2
13+
:class: singlecol
14+
15+
Overview
16+
--------
17+
18+
In this guide, you can learn how to create BSON documents, read BSON from a file,
19+
and write BSON to a file by using {+driver-short+}.
20+
21+
**BSON**, or Binary JSON, is the data format that MongoDB uses to organize
22+
and store data. This data format includes all JSON data structure types and
23+
adds support for types including dates, different size integers, ObjectIds, and
24+
binary data. You can use BSON documents in your {+language+} application by including the
25+
`bson <{+api-root+}bson/index.html>`__ package. For a complete list of supported types, see the
26+
:manual:`BSON Types </reference/bson-types>` server manual page.
27+
28+
The code samples in this guide use the following BSON document as an example:
29+
30+
.. code-block:: none
31+
32+
{
33+
"address" : {
34+
"street" : "Pizza St",
35+
"zipcode" : "10003"
36+
},
37+
"coord" : [-73.982419, 41.579505]
38+
"cuisine" : "Pizza",
39+
"name" : "Mongo's Pizza"
40+
}
41+
42+
Create a BSON Document
43+
----------------------
44+
45+
You can create a BSON document by using the same notation you use to create a
46+
dictionary in {+language+}. {+driver-short+} automatically converts {+language+} dictionaries
47+
into BSON documents when inserting them into a collection.
48+
49+
The following example creates a BSON document that
50+
represents the preceding sample BSON document:
51+
52+
.. code-block:: python
53+
54+
document = {
55+
"address": {
56+
"street": "Pizza St",
57+
"zipcode": "10003"
58+
},
59+
"coord": [-73.982419, 41.579505],
60+
"cuisine": "Pizza",
61+
"name": "Mongo's Pizza"
62+
}
63+
64+
Change a BSON Document
65+
----------------------
66+
67+
You can modify the contents of a BSON document by using the same notation you use to modify
68+
a dictionary in {+language+}. The following example makes three changes to the previous
69+
BSON document:
70+
71+
1. Adds a new field, ``restaurant_id``, with the value ``12345``
72+
#. Removes the ``cuisine`` field
73+
#. Sets the value of the ``name`` field to ``"Mongo's Pizza Place"``
74+
75+
.. code-block:: python
76+
77+
document["restaurant_id"] = 12345
78+
del document["cuisine"]
79+
document["name"] = "Mongo's Pizza Place"
80+
81+
Write BSON to a File
82+
--------------------
83+
84+
To write BSON data to a file, open a file stream in write-binary mode on the output file.
85+
Then, write each document to the output file. Ensure that documents are encoded in BSON
86+
format by using the ``bson.encode()`` method.
87+
88+
The following example writes the sample BSON document to ``file.bson``:
89+
90+
.. code-block:: python
91+
92+
with open("file.bson", "w") as file:
93+
file.write(bson.encode(document))
94+
95+
Read BSON from a File
96+
---------------------
97+
98+
To read BSON documents from a file, open a file stream in read-binary mode on the input
99+
file. Then, decode the documents from BSON format as you read them by using the ``bson.decode()``
100+
method.
101+
102+
The following example reads the sample BSON document from ``file.bson``:
103+
104+
.. io-code-block::
105+
:copyable: true
106+
107+
.. input::
108+
:language: python
109+
110+
with open("file.bson", "rb") as file:
111+
data = file.read()
112+
document = bson.decode(data)
113+
print(document)
114+
115+
.. output::
116+
:visible: false
117+
118+
{"address": {"street": "Pizza St", "zipcode": "10003"}, "coord": [-73.982419, 41.579505], "cuisine": "Pizza", "name": "Mongo's Pizza"}
119+
120+
API Documentation
121+
-----------------
122+
123+
To learn more about any of the methods or types discussed in this
124+
guide, see the `bson <{+api-root+}bson/index.html>`__ API documentation.

0 commit comments

Comments
 (0)