@@ -16,7 +16,7 @@ Definition
16
16
.. method:: db.collection.insertMany()
17
17
18
18
.. versionadded:: 3.2
19
-
19
+
20
20
Inserts multiple documents into a collection.
21
21
22
22
The :method:`~db.collection.insertMany()` method has the following
@@ -35,28 +35,28 @@ Definition
35
35
.. include:: /includes/apiargs/method-db.collection.insertMany-param.rst
36
36
37
37
:returns:
38
-
38
+
39
39
A document containing:
40
40
41
- - A boolean ``acknowledged`` as ``true`` if the operation ran with
41
+ - A boolean ``acknowledged`` as ``true`` if the operation ran with
42
42
:term:`write concern` or ``false`` if write concern was disabled
43
43
44
44
- An array of ``_id`` for each successfully inserted documents
45
45
46
46
Behaviors
47
47
---------
48
48
49
- Given an array of documents, :method:`~db.collection.insertMany()`
49
+ Given an array of documents, :method:`~db.collection.insertMany()`
50
50
inserts each document in the array into the collection.
51
51
52
52
Execution of Operations
53
53
~~~~~~~~~~~~~~~~~~~~~~~
54
54
55
55
By default documents are inserted in order.
56
56
57
- If ``ordered`` is set to false, documents are inserted in an unordered
58
- format and may be reordered by :program:`mongod` to increase performance.
59
- Applications should not depend on ordering of inserts if using an unordered
57
+ If ``ordered`` is set to false, documents are inserted in an unordered
58
+ format and may be reordered by :program:`mongod` to increase performance.
59
+ Applications should not depend on ordering of inserts if using an unordered
60
60
:method:`~db.collection.insertMany()`.
61
61
62
62
.. include:: /includes/fact-bulkwrite-operation-batches.rst
@@ -67,13 +67,13 @@ Applications should not depend on ordering of inserts if using an unordered
67
67
Collection Creation
68
68
~~~~~~~~~~~~~~~~~~~
69
69
70
- If the collection does not exist, then :method:`~db.collection.insertMany()`
70
+ If the collection does not exist, then :method:`~db.collection.insertMany()`
71
71
creates the collection on successful write.
72
72
73
73
``_id`` Field
74
74
~~~~~~~~~~~~~
75
75
76
- If the document does not specify an :term:`_id` field, then :program:`mongod`
76
+ If the document does not specify an :term:`_id` field, then :program:`mongod`
77
77
adds the ``_id`` field and assign a unique
78
78
:method:`ObjectId` for the document. Most
79
79
drivers create an ObjectId and insert the ``_id`` field, but the
@@ -100,16 +100,18 @@ Error Handling
100
100
101
101
Inserts throw a ``BulkWriteError`` exception.
102
102
103
- Excluding :doc:`/reference/write-concern` errors, ordered operations stop
104
- after an error, while unordered operations continue to process any
103
+ Excluding :doc:`/reference/write-concern` errors, ordered operations stop
104
+ after an error, while unordered operations continue to process any
105
105
remaining write operations in the queue.
106
106
107
- Write concern errors are displayed in the ``writeConcernErrors`` field, while
108
- all other errors are displayed in the ``writeErrors`` field. If an error is
109
- encountered, the number of successful write operations are displayed instead
110
- of a list of inserted _ids. Ordered operations display the single error
107
+ Write concern errors are displayed in the ``writeConcernErrors`` field, while
108
+ all other errors are displayed in the ``writeErrors`` field. If an error is
109
+ encountered, the number of successful write operations are displayed instead
110
+ of a list of inserted _ids. Ordered operations display the single error
111
111
encountered while unordered operations display each error in an array.
112
112
113
+ .. _insertMany-examples:
114
+
113
115
Examples
114
116
--------
115
117
@@ -119,13 +121,13 @@ collection.
119
121
Insert Several Document without Specifying an ``_id`` Field
120
122
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
121
123
122
- The following example uses :method:`db.collection.insertMany()` to insert
124
+ The following example uses :method:`db.collection.insertMany()` to insert
123
125
documents that do not contain the ``_id`` field:
124
126
125
127
.. code-block:: javascript
126
128
127
129
try {
128
- db.products.insertMany( [
130
+ db.products.insertMany( [
129
131
{ item: "card", qty: 15 },
130
132
{ item: "envelope", qty: 20 },
131
133
{ item: "stamps" , qty: 30 }
@@ -147,7 +149,7 @@ The operation returns the following document:
147
149
]
148
150
}
149
151
150
- Because the documents did not include ``_id``,
152
+ Because the documents did not include ``_id``,
151
153
:program:`mongod` creates and adds the ``_id`` field for each document and
152
154
assigns it a unique :method:`ObjectId` value.
153
155
@@ -156,8 +158,8 @@ assigns it a unique :method:`ObjectId` value.
156
158
Insert Several Document Specifying an ``_id`` Field
157
159
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
158
160
159
- The following example/operation uses :method:`~db.collection.insertMany()` to
160
- insert documents that include the ``_id`` field. The value of ``_id`` must be
161
+ The following example/operation uses :method:`~db.collection.insertMany()` to
162
+ insert documents that include the ``_id`` field. The value of ``_id`` must be
161
163
unique within the collection to avoid a duplicate key error.
162
164
163
165
.. code-block:: javascript
@@ -178,8 +180,8 @@ The operation returns the following document:
178
180
179
181
{ "acknowledged" : true, "insertedIds" : [ 10, 11, 12 ] }
180
182
181
- Inserting a duplicate value for any key that is part of a :term:`unique
182
- index`, such as ``_id``, throws an exception. The following attempts to insert
183
+ Inserting a duplicate value for any key that is part of a :term:`unique
184
+ index`, such as ``_id``, throws an exception. The following attempts to insert
183
185
a document with a ``_id`` value that already exists:
184
186
185
187
.. code-block:: javascript
@@ -193,11 +195,11 @@ a document with a ``_id`` value that already exists:
193
195
} catch (e) {
194
196
print (e);
195
197
}
196
-
198
+
197
199
Since ``_id: 13`` already exists, the following exception is thrown:
198
-
200
+
199
201
.. code-block:: javascript
200
-
202
+
201
203
BulkWriteError({
202
204
"writeErrors" : [
203
205
{
@@ -220,19 +222,19 @@ Since ``_id: 13`` already exists, the following exception is thrown:
220
222
"upserted" : [ ]
221
223
})
222
224
223
- Note that one document was inserted: The first document of ``_id: 13`` will
224
- insert successfully, but the second insert will fail. This will also stop
225
+ Note that one document was inserted: The first document of ``_id: 13`` will
226
+ insert successfully, but the second insert will fail. This will also stop
225
227
additional documents left in the queue from being inserted.
226
228
227
- With ``ordered`` to ``false``, the insert operation would continue with any
229
+ With ``ordered`` to ``false``, the insert operation would continue with any
228
230
remaining documents.
229
231
230
232
Unordered Inserts
231
233
~~~~~~~~~~~~~~~~~
232
234
233
- The following attempts to insert multiple documents with ``_id`` field and
234
- ``ordered: false``. The array of documents contains two documents with
235
- duplicate ``_id`` fields.
235
+ The following attempts to insert multiple documents with ``_id`` field and
236
+ ``ordered: false``. The array of documents contains two documents with
237
+ duplicate ``_id`` fields.
236
238
237
239
.. code-block:: javascript
238
240
@@ -253,7 +255,7 @@ duplicate ``_id`` fields.
253
255
The operation throws the following exception:
254
256
255
257
.. code-block:: javascript
256
-
258
+
257
259
BulkWriteError({
258
260
"writeErrors" : [
259
261
{
@@ -286,22 +288,22 @@ The operation throws the following exception:
286
288
"upserted" : [ ]
287
289
})
288
290
289
- While the document with ``item: "medium box"`` and ``item: "tape"``
290
- failed to insert due to duplicate ``_id`` values,
291
+ While the document with ``item: "medium box"`` and ``item: "tape"``
292
+ failed to insert due to duplicate ``_id`` values,
291
293
``nInserted`` shows that the remaining 5 documents were inserted.
292
294
293
295
.. _insertMany-using-write-concern:
294
296
295
297
Using Write Concern
296
298
~~~~~~~~~~~~~~~~~~~~~~~~
297
299
298
- Given a three member replica set, the following operation specifies a
300
+ Given a three member replica set, the following operation specifies a
299
301
``w`` of ``majority`` and ``wtimeout`` of ``100``:
300
302
301
303
.. code-block:: javascript
302
304
303
305
try {
304
- db.products.insertMany(
306
+ db.products.insertMany(
305
307
[
306
308
{ _id: 10, item: "large box", qty: 20 },
307
309
{ _id: 11, item: "small box", qty: 55 },
@@ -313,7 +315,7 @@ Given a three member replica set, the following operation specifies a
313
315
print (e);
314
316
}
315
317
316
- If the primary and at least one secondary acknowledge each write operation
318
+ If the primary and at least one secondary acknowledge each write operation
317
319
within 100 milliseconds, it returns:
318
320
319
321
.. code-block:: javascript
@@ -327,9 +329,9 @@ within 100 milliseconds, it returns:
327
329
]
328
330
}
329
331
330
- If the total time required for all required nodes in the replica set to
331
- acknowledge the write operation is greater than ``wtimeout``,
332
- the following ``writeConcernError`` is displayed when the ``wtimeout`` period
332
+ If the total time required for all required nodes in the replica set to
333
+ acknowledge the write operation is greater than ``wtimeout``,
334
+ the following ``writeConcernError`` is displayed when the ``wtimeout`` period
333
335
has passed.
334
336
335
337
This operation returns:
0 commit comments