-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathdb.collection.insert.txt
344 lines (235 loc) · 9.13 KB
/
db.collection.insert.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
======================
db.collection.insert()
======================
.. default-domain:: mongodb
.. meta::
:keywords: deprecated
:description: The insert method is deprecated should be replaced by insertOne or insertMany.
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
.. include:: /includes/fact-mongosh-shell-method-deprecated.rst
Definition
----------
.. method:: db.collection.insert()
Inserts a document or documents into a collection.
:returns:
- A :ref:`writeresults-insert` object for single inserts.
- A :ref:`bulkwriteresults-insert` object for bulk inserts.
Syntax
------
The :method:`~db.collection.insert()` method has the following
syntax:
.. code-block:: none
db.collection.insert(
<document or array of documents>,
{
writeConcern: <document>,
ordered: <boolean>
}
)
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Parameter
- Type
- Description
* - ``document``
- document or array
- A document or array of documents to insert into the collection.
* - ``writeConcern``
- document
- Optional. A document expressing the :doc:`write concern
</reference/write-concern>`. Omit to use the default write
concern. See :ref:`insert-wc`.
.. include:: /includes/extracts/transactions-operations-write-concern.rst
* - ``ordered``
- boolean
- Optional. If ``true``, perform an ordered insert of the
documents in the
array, and if an error occurs with one of documents, MongoDB will
return without processing the remaining documents in the array.
If ``false``, perform an unordered insert, and if an error occurs
with one of documents, continue processing the remaining
documents in the array.
Defaults to ``true``.
The :method:`~db.collection.insert()` returns an object that
contains the status of the operation.
Behaviors
---------
.. _insert-wc:
Write Concern
~~~~~~~~~~~~~
The :method:`~db.collection.insert()` method uses the
:dbcommand:`insert` command, which uses the default :doc:`write concern
</reference/write-concern>`. To specify a different write concern,
include the write concern in the options parameter.
Create Collection
~~~~~~~~~~~~~~~~~
If the collection does not exist, then the
:method:`~db.collection.insert()` method will create the collection.
``_id`` Field
~~~~~~~~~~~~~
If the document does not specify an :term:`_id` field, then MongoDB
will add the ``_id`` field and assign a unique
:method:`ObjectId` for the document before inserting. Most
drivers create an ObjectId and insert the ``_id`` field, but the
:binary:`~bin.mongod` will create and populate the ``_id`` if the driver or
application does not.
If the document contains an ``_id`` field, the ``_id`` value must be
unique within the collection to avoid duplicate key error.
Transactions
~~~~~~~~~~~~
.. include:: /includes/extracts/transactions-supported-operation.rst
.. include:: /includes/extracts/transactions-usage.rst
Collection Creation in Transactions
````````````````````````````````````
.. include:: /includes/extracts/transactions-insert-implicit-collection-creation.rst
Write Concerns and Transactions
````````````````````````````````
.. include:: /includes/extracts/transactions-operations-write-concern.rst
.. |operation| replace:: :method:`db.collection.insert()`
Oplog Entries
~~~~~~~~~~~~~
If a ``db.collection.insert()`` operation successfully inserts a document,
the operation adds an entry on the :term:`oplog` (operations log).
If the operation fails, the operation does not add an entry on the
oplog.
Examples
--------
The following examples insert documents into the ``products``
collection. If the collection does not exist, the
:method:`~db.collection.insert()` method creates the collection.
Insert a Document without Specifying an ``_id`` Field
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the following example, the document passed to the
:method:`~db.collection.insert()` method does not contain the ``_id``
field:
.. code-block:: javascript
db.products.insert( { item: "card", qty: 15 } )
During the insert, :binary:`~bin.mongod` will create the ``_id`` field and
assign it a unique :method:`ObjectId` value, as verified by
the inserted document:
.. code-block:: javascript
{ "_id" : ObjectId("5063114bd386d8fadbd6b004"), "item" : "card", "qty" : 15 }
.. include:: /includes/fact-object-id-may-differ.rst
Insert a Document Specifying an ``_id`` Field
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the following example, the document passed to the
:method:`~db.collection.insert()` method includes the ``_id`` field.
The value of ``_id`` must be unique within the collection to avoid
duplicate key error.
.. code-block:: javascript
db.products.insert( { _id: 10, item: "box", qty: 20 } )
The operation inserts the following document in the ``products``
collection:
.. code-block:: javascript
{ "_id" : 10, "item" : "box", "qty" : 20 }
Insert Multiple Documents
~~~~~~~~~~~~~~~~~~~~~~~~~
The following example performs a bulk insert of three documents by
passing an array of documents to the :method:`~db.collection.insert()`
method. By default, MongoDB performs an *ordered* insert. With
*ordered* inserts, if an error occurs during an insert of one of the
documents, MongoDB returns on error without processing the remaining
documents in the array.
The documents in the array do not need to have the same fields. For
instance, the first document in the array has an ``_id`` field and a
``type`` field. Because the second and third documents do not contain
an ``_id`` field, :binary:`~bin.mongod` will create the ``_id`` field for
the second and third documents during the insert:
.. code-block:: javascript
db.products.insert(
[
{ _id: 11, item: "pencil", qty: 50, type: "no.2" },
{ item: "pen", qty: 20 },
{ item: "eraser", qty: 25 }
]
)
The operation inserted the following three documents:
.. code-block:: javascript
{ "_id" : 11, "item" : "pencil", "qty" : 50, "type" : "no.2" }
{ "_id" : ObjectId("51e0373c6f35bd826f47e9a0"), "item" : "pen", "qty" : 20 }
{ "_id" : ObjectId("51e0373c6f35bd826f47e9a1"), "item" : "eraser", "qty" : 25 }
Perform an Unordered Insert
~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following example performs an *unordered* insert of three
documents. With *unordered* inserts, if an error occurs during an
insert of one of the documents, MongoDB continues to insert the
remaining documents in the array.
.. code-block:: javascript
db.products.insert(
[
{ _id: 20, item: "lamp", qty: 50, type: "desk" },
{ _id: 21, item: "lamp", qty: 20, type: "floor" },
{ _id: 22, item: "bulk", qty: 100 }
],
{ ordered: false }
)
Override Default Write Concern
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following operation to a replica set specifies a :doc:`write concern
</reference/write-concern>` of ``w: 2`` with a ``wtimeout`` of 5000
milliseconds. This operation either returns after the write propagates
to both the primary and one secondary, or times out after 5 seconds.
.. code-block:: javascript
db.products.insert(
{ item: "envelopes", qty : 100, type: "Clasp" },
{ writeConcern: { w: 2, wtimeout: 5000 } }
)
.. _writeresults-insert:
WriteResult
-----------
When passed a single document, :method:`~db.collection.insert()`
returns a ``WriteResult`` object.
Successful Results
~~~~~~~~~~~~~~~~~~
The :method:`~db.collection.insert()` returns a :method:`WriteResult`
object that contains the status of the operation. Upon success, the
:method:`WriteResult` object contains information on the number of
documents inserted:
.. code-block:: javascript
WriteResult({ "nInserted" : 1 })
Write Concern Errors
~~~~~~~~~~~~~~~~~~~~
If the :method:`~db.collection.insert()` method encounters write
concern errors, the results include the
:data:`WriteResult.writeConcernError` field:
.. code-block:: javascript
WriteResult({
"nInserted" : 1,
"writeConcernError"({
"code" : 64,
"errmsg" : "waiting for replication timed out",
"errInfo" : {
"wtimeout" : true,
"writeConcern" : {
"w" : "majority",
"wtimeout" : 100,
"provenance" : "getLastErrorDefaults"
}
}
})
.. seealso::
:data:`WriteResult.writeConcernError`
Errors Unrelated to Write Concern
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If the :method:`~db.collection.insert()` method encounters a non-write
concern error, the results include the :data:`WriteResult.writeError`
field:
.. code-block:: javascript
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: test.foo.$_id_ dup key: { : 1.0 }"
}
})
.. _bulkwriteresults-insert:
BulkWriteResult
---------------
When passed an array of documents, :method:`~db.collection.insert()`
returns a :method:`BulkWriteResult()` object. See
:method:`BulkWriteResult()` for details.