Skip to content

Commit cc2db82

Browse files
committed
wip
1 parent c7d2abd commit cc2db82

File tree

7 files changed

+78
-290
lines changed

7 files changed

+78
-290
lines changed

config/redirects

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ raw: ${prefix}/master -> ${base}/upcoming/
1212
raw: ${prefix}/get-started/download-and-install/ -> ${base}/current/get-started/download-and-install/
1313

1414
[*-master]: ${prefix}/${version}/security/enterprise-authentication/ -> ${base}/${version}/security/authentication/
15+
[*-master]: ${prefix}/${version}/faq/ -> ${base}/${version}/

source/connect/mongoclient.txt

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,60 @@ constructor accepts. All parameters are optional.
168168

169169
**Data type:** `TypeRegistry <{+api-root+}bson/codec_options.html#bson.codec_options.TypeRegistry>`__
170170

171-
.. tip:: Reusing Your Client
171+
Concurrent Execution
172+
--------------------
172173

173-
Because each ``MongoClient`` object represents a pool of connections to the
174-
database, most applications require only a single instance of
175-
``MongoClient``, even across multiple requests. However, if you fork
176-
a process, the child process *does* need its own ``MongoClient`` object.
177-
To learn more, see the :ref:`FAQ <pymongo-faq>` page.
174+
The following sections describe {+driver-short+}'s support for concurrent execution
175+
mechanisms.
176+
177+
Multithreading
178+
~~~~~~~~~~~~~~
179+
180+
{+driver-short+} is thread-safe and provides built-in connection pooling
181+
for threaded applications.
182+
Because each ``MongoClient`` object represents a pool of connections to the
183+
database, most applications require only a single instance of
184+
``MongoClient``, even across multiple requests.
185+
186+
.. _pymongo-forks:
187+
188+
Multiple Forks
189+
~~~~~~~~~~~~~~~
190+
191+
{+driver-short+} supports using the ``fork()`` method to create a new process.
192+
However, if you fork a process, you must create a new ``MongoClient`` instance in the
193+
child process.
194+
195+
.. important:: Don't Pass a MongoClient to a Child Process
196+
197+
If you use the ``fork()`` method to create a new process, don't pass an instance
198+
of the ``MongoClient`` class from the parent process to the child process. This creates
199+
a high probability of deadlock among ``MongoClient`` instances in the child process.
200+
{+driver-short+} tries to issue a warning if this deadlock might occur.
201+
202+
Multiprocessing
203+
~~~~~~~~~~~~~~~
204+
205+
{+driver-short+} supports the Python ``multiprocessing`` module.
206+
However, on Unix systems, the multiprocessing module spawns processes by using
207+
the ``fork()`` method. This carries the same risks described in :ref:`<pymongo-forks>`
208+
209+
To use multiprocessing with {+driver-short+}, write code similar to the following example:
210+
211+
.. code-block:: python
212+
213+
# Each process creates its own instance of MongoClient.
214+
def func():
215+
db = pymongo.MongoClient().mydb
216+
# Do something with db.
217+
218+
proc = multiprocessing.Process(target=func)
219+
proc.start()
220+
221+
.. important::
222+
223+
Do not copy an instance of the ``MongoClient`` class from the parent process to a child
224+
process.
178225

179226
Type Hints
180227
----------

source/faq.txt

Lines changed: 1 addition & 249 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,4 @@
1-
.. _pymongo-faq:
2-
3-
Frequently Asked Questions
4-
==========================
5-
6-
.. contents:: On this page
7-
:local:
8-
:backlinks: none
9-
:depth: 1
10-
:class: singlecol
11-
12-
.. facet::
13-
:name: genre
14-
:values: reference
15-
16-
.. meta::
17-
:keywords: errors, problems, help, troubleshoot
18-
19-
Is {+driver-short+} Thread-Safe?
20-
-----------------------
21-
22-
Yes. {+driver-short+} is thread-safe and provides built-in connection pooling
23-
for threaded applications.
24-
25-
.. _pymongo-fork-safe:
26-
27-
Is {+driver-short+} Fork-Safe?
28-
---------------------
29-
30-
No. If you use the ``fork()`` method to create a new process, don't pass an instance
31-
of the ``MongoClient`` class from the parent process to the child process. This creates
32-
a high probability of deadlock among ``MongoClient`` instances in the child process.
33-
Instead, create a new ``MongoClient`` instance in the child process.
34-
35-
.. note::
36-
37-
{+driver-short+} tries to issue a warning if this deadlock might occur.
38-
39-
Can I Use {+driver-short+} with Multiprocessing?
40-
---------------------------------------
41-
42-
Yes. However, on Unix systems, the multiprocessing module spawns processes by using
43-
the ``fork()`` method. This carries the same risks described in :ref:`<pymongo-fork-safe>`
44-
45-
To use multiprocessing with {+driver-short+}, write code similar to the following example:
46-
47-
.. code-block:: python
48-
49-
# Each process creates its own instance of MongoClient.
50-
def func():
51-
db = pymongo.MongoClient().mydb
52-
# Do something with db.
53-
54-
proc = multiprocessing.Process(target=func)
55-
proc.start()
56-
57-
.. important::
58-
59-
Do not copy an instance of the ``MongoClient`` class from the parent process to a child
60-
process.
1+
.. docs-landing/source/languages/python.txt
612

623
Can {+driver-short+} Load the Results of a Query as a Pandas DataFrame?
634
-----------------------------------------------------------------------
@@ -69,194 +10,6 @@ load MongoDB query result-sets as
6910
`NumPy ndarrays <https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html>`__, or
7011
`Apache Arrow Tables <https://arrow.apache.org/docs/python/generated/pyarrow.Table.html>`__.
7112

72-
How Does Connection Pooling Work in {+driver-short+}?
73-
--------------------------------------------
74-
75-
Every ``MongoClient`` instance has a built-in connection pool for each server
76-
in your MongoDB topology. Connection pools open sockets on demand to
77-
support concurrent requests to MongoDB in your application.
78-
79-
The maximum size of each connection pool is set by the ``maxPoolSize`` option, which
80-
defaults to ``100``. If the number of in-use connections to a server reaches
81-
the value of ``maxPoolSize``, the next request to that server will wait
82-
until a connection becomes available.
83-
84-
In addition to the sockets needed to support your application's requests,
85-
each ``MongoClient`` instance opens two more sockets per server
86-
in your MongoDB topology for monitoring the server's state.
87-
For example, a client connected to a three-node replica set opens six
88-
monitoring sockets. If the application uses the default setting for
89-
``maxPoolSize`` and only queries the primary (default) node, then
90-
there can be at most ``106`` total connections in the connection pool. If the
91-
application uses a :ref:`read preference <read-preference>` to query the
92-
secondary nodes, those connection pools grow and there can be
93-
``306`` total connections.
94-
95-
To support high numbers of concurrent MongoDB requests
96-
within one process, you can increase ``maxPoolSize``.
97-
98-
Connection pools are rate-limited. The ``maxConnecting`` option
99-
determines the number of connections that the pool can create in
100-
parallel at any time. For example, if the value of ``maxConnecting`` is
101-
``2``, the third request that attempts to concurrently check out a
102-
connection succeeds only when one the following cases occurs:
103-
104-
- The connection pool finishes creating a connection and there are fewer
105-
than ``maxPoolSize`` connections in the pool.
106-
- An existing connection is checked back into the pool.
107-
- The driver's ability to reuse existing connections improves due to
108-
rate-limits on connection creation.
109-
110-
You can set the minimum number of concurrent connections to
111-
each server with the ``minPoolSize`` option, which defaults to ``0``.
112-
The driver initializes the connection pool with this number of sockets. If
113-
sockets are closed, causing the total number
114-
of sockets (both in use and idle) to drop below the minimum, more
115-
sockets are opened until the minimum is reached.
116-
117-
You can set the maximum number of milliseconds that a connection can
118-
remain idle in the pool by setting the ``maxIdleTimeMS`` option.
119-
Once a connection has been idle for ``maxIdleTimeMS``, the connection
120-
pool removes and replaces it. This option defaults to ``0`` (no limit).
121-
122-
The following default configuration for a ``MongoClient`` works for most
123-
applications:
124-
125-
.. code-block:: python
126-
127-
client = MongoClient(host, port)
128-
129-
``MongoClient`` supports multiple concurrent requests. For each process,
130-
create a client and reuse it for all operations in a process. This
131-
practice is more efficient than creating a client for each request.
132-
133-
The driver does not limit the number of requests that
134-
can wait for sockets to become available, and it is the application's
135-
responsibility to limit the size of its pool to bound queuing
136-
during a load spike. Requests wait for the amount of time specified in
137-
the ``waitQueueTimeoutMS`` option, which defaults to ``0`` (no limit).
138-
139-
A request that waits more than the length of time defined by
140-
``waitQueueTimeoutMS`` for a socket raises a ``ConnectionFailure`` error. Use this
141-
option if it is more important to bound the duration of operations
142-
during a load spike than it is to complete every operation.
143-
144-
When ``MongoClient.close()`` is called by any request, the driver
145-
closes all idle sockets and closes all sockets that are in
146-
use as they are returned to the pool. Calling ``MongoClient.close()``
147-
closes only inactive sockets, so you cannot interrupt or terminate
148-
any ongoing operations by using this method. The driver closes these
149-
sockets only when the process completes.
150-
151-
For more information, see the :manual:`Connection Pool Overview </administration/connection-pool-overview/>`
152-
in the {+mdb-server+} documentation.
153-
154-
Why Does {+driver-short+} Add an _id Field to All My Documents?
155-
------------------------------------------------------
156-
157-
When you use the ``Collection.insert_one()`` method,
158-
``Collection.insert_many()`` method, or
159-
``Collection.bulk_write()`` method to insert a document into MongoDB,
160-
and that document does not
161-
include an ``_id`` field, {+driver-short+} automatically adds this field for you.
162-
It also sets the value of the field to an instance of ``ObjectId``.
163-
164-
The following code example inserts a document without an ``_id`` field into MongoDB, then
165-
prints the document. After it's inserted, the document contains an ``_id`` field whose
166-
value is an instance of ``ObjectId``.
167-
168-
.. code-block:: python
169-
170-
>>> my_doc = {'x': 1}
171-
>>> collection.insert_one(my_doc)
172-
InsertOneResult(ObjectId('560db337fba522189f171720'), acknowledged=True)
173-
>>> my_doc
174-
{'x': 1, '_id': ObjectId('560db337fba522189f171720')}
175-
176-
{+driver-short+} adds an ``_id`` field in this manner for a few reasons:
177-
178-
- All MongoDB documents must have an ``_id`` field.
179-
- If {+driver-short+} inserts a document without an ``_id`` field, MongoDB adds one
180-
itself, but doesn't report the value back to {+driver-short+} for your application
181-
to use.
182-
- Copying the document before adding the ``_id`` field is
183-
prohibitively expensive for most high-write-volume applications.
184-
185-
.. tip::
186-
187-
If you don't want {+driver-short+} to add an ``_id`` to your documents, insert only
188-
documents that your application has already added an ``_id`` field to.
189-
190-
How Do I Change the Timeout Value for Cursors?
191-
----------------------------------------------
192-
193-
MongoDB doesn't support custom timeouts for cursors, but you can turn off cursor
194-
timeouts. To do so, pass the ``no_cursor_timeout=True`` option to
195-
the ``find()`` method.
196-
197-
How Can I Store ``Decimal`` Instances?
198-
--------------------------------------
199-
200-
MongoDB v3.4 introduced the ``Decimal128`` BSON type, a 128-bit decimal-based
201-
floating-point value capable of emulating decimal rounding with exact precision.
202-
{+driver-short+} versions 3.4 and later also support this type.
203-
Earlier MongoDB versions, however, support only IEEE 754 floating points, equivalent to the
204-
Python ``float`` type. {+driver-short+} can store ``Decimal`` instances to
205-
these versions of MongoDB only by converting them to the ``float`` type.
206-
You must perform this conversion explicitly.
207-
208-
For more information, see the {+driver-short+} API documentation for
209-
`decimal128. <https://pymongo.readthedocs.io/en/latest/api/bson/decimal128.html#module-bson.decimal128>`__
210-
211-
Why Does {+driver-short+} Convert ``9.99`` to ``9.9900000000000002``?
212-
---------------------------------------------------------------------
213-
214-
MongoDB represents ``9.99`` as an IEEE floating-point value, which can't
215-
represent the value precisely. This is also true in some versions of
216-
Python. In this regard, {+driver-short+} behaves the same way as
217-
the JavaScript shell, all other MongoDB drivers, and the Python language itself.
218-
219-
Does {+driver-short+} Support Attribute-style Access for Documents?
220-
----------------------------------------------------------
221-
222-
No. {+driver-short+} doesn't implement this feature, for the following reasons:
223-
224-
1. Adding attributes pollutes the attribute namespace for documents and could
225-
lead to subtle bugs or confusing errors when using a key with the
226-
same name as a dictionary method.
227-
228-
#. {+driver-short+} uses SON objects instead of regular
229-
dictionaries only to maintain key ordering, because the server
230-
requires this for certain operations. Adding this feature would
231-
complicate the ``SON`` class and could break backwards compatibility
232-
if {+driver-short+} ever reverts to using dictionaries.
233-
234-
#. Documents behave just like dictionaries, which makes them relatively simple
235-
for new {+driver-short+} users to understand. Changing the behavior of documents
236-
adds a barrier to entry for these users.
237-
238-
For more information, see the relevant
239-
`Jira case. <http://jira.mongodb.org/browse/PYTHON-35>`__
240-
241-
Does {+driver-short+} Support Asynchronous Frameworks?
242-
---------------------------------------------
243-
244-
Yes. For more information, see the :ref:`<pymongo-tools>` guide.
245-
246-
Does {+driver-short+} Work with mod_wsgi?
247-
--------------------------------
248-
249-
Yes. See :ref:`pymongo-mod_wsgi` in the Tools guide.
250-
251-
Does {+driver-short+} Work with PythonAnywhere?
252-
--------------------------------------
253-
254-
No. {+driver-short+} creates Python threads, which
255-
`PythonAnywhere <https://www.pythonanywhere.com>`__ does not support.
256-
257-
For more information, see
258-
the relevant `Jira ticket. <https://jira.mongodb.org/browse/PYTHON-1495>`__
259-
26013
How Can I Encode My Documents to JSON?
26114
--------------------------------------
26215

@@ -278,7 +31,6 @@ depend on {+driver-short+} and might offer a performance improvement over
27831
python-bsonjs works best with {+driver-short+} when using the ``RawBSONDocument``
27932
type.
28033

281-
28234
Does {+driver-short+} Behave Differently in Python 3?
28335
-----------------------------------------------------
28436

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.. note:: _id Field Must Be Unique
2+
3+
In a MongoDB collection, each document must contain an ``_id`` field
4+
with a unique value.
5+
6+
If you specify a value for the ``_id`` field, you must ensure that the
7+
value is unique across the collection. If you don't specify a value,
8+
the driver automatically generates a unique ``ObjectId`` value for the field.
9+
10+
We recommend letting the driver automatically generate ``_id`` values to
11+
ensure uniqueness. Duplicate ``_id`` values violate unique index constraints, which
12+
causes the driver to return an error.

source/tools.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,11 @@ This section lists alternatives to {+driver-short+}.
274274
- `MongoMock <https://github.com/mongomock/mongomock>`__ is a small
275275
library to help test Python code. It uses {+driver-short+} to interact with MongoDB.
276276

277+
.. note:: {+driver-short+} is Incompatible with PythonAnywhere
278+
279+
{+driver-short+} creates Python threads, which
280+
`PythonAnywhere <https://www.pythonanywhere.com>`__ does not support.
281+
282+
For more information, see
283+
the relevant `Jira ticket. <https://jira.mongodb.org/browse/PYTHON-1495>`__
284+

source/write/bulk-write.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,7 @@ The following example creates an instance of ``InsertOne``:
6767

6868
To insert multiple documents, create an instance of ``InsertOne`` for each document.
6969

70-
.. note::
71-
72-
Duplicate ``_id`` values violate unique index constraints, which causes the
73-
driver to return a ``DuplicateKeyError``. To avoid this error, ensure that
74-
each document you insert has a unique ``_id`` value.
70+
.. include:: /includes/write/unique-id-note.rst
7571

7672
Update Operations
7773
~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)