-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathinsert.txt
295 lines (182 loc) · 6.91 KB
/
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
======
insert
======
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. dbcommand:: insert
The :dbcommand:`insert` command inserts one or more documents and
returns a document containing the status of all inserts. The insert
methods provided by the MongoDB drivers use this command internally.
The command has the following syntax:
.. code-block:: javascript
{
insert: <collection>,
documents: [ <document>, <document>, <document>, ... ],
ordered: <boolean>,
writeConcern: { <write concern> },
bypassDocumentValidation: <boolean>,
comment: <any>,
maxTimeMS: <int>
}
The :dbcommand:`insert` command takes the following fields:
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Field
- Type
- Description
* - ``insert``
- string
- The name of the target collection.
* - ``documents``
- array
- An array of one or more documents to insert into the named collection.
* - ``ordered``
- boolean
- Optional. If ``true``, then when an insert of a document fails, return without
inserting any remaining documents listed in the ``inserts`` array. If
``false``, then when an insert of a document fails, continue to insert the
remaining documents. Defaults to ``true``.
* - ``writeConcern``
- document
- Optional. A document that expresses the :doc:`write concern </reference/write-concern>`
of the :dbcommand:`insert` command. Omit to use the default write
concern.
.. include:: /includes/extracts/transactions-operations-write-concern.rst
* - ``bypassDocumentValidation``
- boolean
- Optional. Enables :dbcommand:`insert` to bypass document validation
during the operation. This lets you insert documents that do not
meet the validation requirements.
.. versionadded:: 3.2
* - ``comment``
- any
- .. include:: /includes/extracts/comment-content.rst
.. versionadded:: 4.4
* - ``maxTimeMS``
- positive integer
- Optional. The cumulative time limit in milliseconds for processing operations on
the cursor. MongoDB aborts the operation at the earliest following
:term:`interrupt point`.
*Supported on mongos since MongoDB 5.0, only supported on mongod in previous versions.
:returns:
A document that contains the status of the operation.
See :ref:`insert-command-output` for details.
Behavior
--------
Size Limit
~~~~~~~~~~
The total size of all the ``documents`` array elements must be less
than or equal to the :limit:`maximum BSON document size
<BSON Document Size>`.
The total number of documents in the ``documents`` array must be less
than or equal to the :limit:`maximum bulk size
<Write Command Batch Limit Size>`.
Document Validation
~~~~~~~~~~~~~~~~~~~
.. include:: /includes/extracts/bypassDocumentValidation-insert.rst
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:: :dbcommand:`insert`
Examples
--------
Insert a Single Document
~~~~~~~~~~~~~~~~~~~~~~~~
Insert a document into the ``users`` collection:
.. code-block:: javascript
db.runCommand(
{
insert: "users",
documents: [ { _id: 1, user: "abc123", status: "A" } ]
}
)
The returned document shows that the command successfully inserted a
document. See :ref:`insert-command-output` for details.
.. code-block:: javascript
{ "ok" : 1, "n" : 1 }
Bulk Insert
~~~~~~~~~~~
Insert three documents into the ``users`` collection:
.. code-block:: javascript
db.runCommand(
{
insert: "users",
documents: [
{ _id: 2, user: "ijk123", status: "A" },
{ _id: 3, user: "xyz123", status: "P" },
{ _id: 4, user: "mop123", status: "P" }
],
ordered: false,
writeConcern: { w: "majority", wtimeout: 5000 }
}
)
The returned document shows that the command successfully inserted the
three documents. See :ref:`insert-command-output` for details.
.. code-block:: javascript
{ "ok" : 1, "n" : 3 }
.. _insert-command-output:
Output
------
The returned document contains a subset of the following fields:
.. data:: insert.ok
The status of the command.
.. data:: insert.n
The number of documents inserted.
.. data:: insert.writeErrors
An array of documents that contains information regarding any error
encountered during the insert operation. The
:data:`~insert.writeErrors` array contains an error document for
each insert that errors.
Each error document contains the following fields:
.. data:: insert.writeErrors.index
An integer that identifies the document in the
``documents`` array, which uses a zero-based index.
.. data:: insert.writeErrors.code
An integer value identifying the error.
.. data:: insert.writeErrors.errmsg
A description of the error.
.. data:: insert.writeConcernError
Document that describe error related to write concern and contains
the field:
.. data:: insert.writeConcernError.code
An integer value identifying the cause of the write concern error.
.. data:: insert.writeConcernError.errmsg
A description of the cause of the write concern error.
.. data:: insert.writeConcernError.errInfo.writeConcern
.. versionadded:: 4.4
.. include:: /includes/fact-errInfo-wc.rst
.. data:: insert.writeConcernError.errInfo.writeConcern.provenance
.. include:: /includes/fact-errInfo-wc-provenance.rst
The following is an example document returned for a successful
:dbcommand:`insert` of a single document:
.. code-block:: javascript
{ ok: 1, n: 1 }
The following is an example document returned for an
:dbcommand:`insert` of two documents that successfully inserted one
document but encountered an error with the other document:
.. code-block:: javascript
{
"ok" : 1,
"n" : 1,
"writeErrors" : [
{
"index" : 1,
"code" : 11000,
"errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: test.users.$_id_ dup key: { : 1.0 }"
}
]
}