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
61
2
62
3
Can {+driver-short+} Load the Results of a Query as a Pandas DataFrame?
63
4
-----------------------------------------------------------------------
@@ -69,194 +10,6 @@ load MongoDB query result-sets as
69
10
`NumPy ndarrays <https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html>`__, or
70
11
`Apache Arrow Tables <https://arrow.apache.org/docs/python/generated/pyarrow.Table.html>`__.
71
12
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
-
260
13
How Can I Encode My Documents to JSON?
261
14
--------------------------------------
262
15
@@ -278,7 +31,6 @@ depend on {+driver-short+} and might offer a performance improvement over
278
31
python-bsonjs works best with {+driver-short+} when using the ``RawBSONDocument``
279
32
type.
280
33
281
-
282
34
Does {+driver-short+} Behave Differently in Python 3?
283
35
-----------------------------------------------------
284
36
0 commit comments