insert
Definition
New in version 2.6.
The insert
command inserts one or more documents andreturns a document containing the status of all inserts. The insertmethods provided by the MongoDB drivers use this command internally.
The command has the following syntax:
- {
- insert: <collection>,
- documents: [ <document>, <document>, <document>, ... ],
- ordered: <boolean>,
- writeConcern: { <write concern> },
- bypassDocumentValidation: <boolean>
- }
The insert
command takes the following fields:
FieldTypeDescriptioninsert
stringThe name of the target collection.documents
arrayAn array of one or more documents to insert into the named collection.ordered
booleanOptional. If true
, then when an insert of a document fails, return withoutinserting any remaining documents listed in the inserts
array. Iffalse
, then when an insert of a document fails, continue to insert theremaining documents. Defaults to true
.writeConcern
documentOptional. A document that expresses the write concernof the insert
command. Omit to use the default writeconcern.
Do not explicitly set the write concern for the operation if run ina transaction. To use write concern with transactions, seeTransactions and Write Concern.bypassDocumentValidation
booleanOptional. Enables insert
to bypass document validationduring the operation. This lets you insert documents that do notmeet the validation requirements.
New in version 3.2.
Returns:A document that contains the status of the operation.See Output for details.
Behavior
Size Limit
The total size of all the documents
array elements must be lessthan or equal to the maximum BSON document size
.
The total number of documents in the documents
array must be lessthan or equal to the maximum bulk size
.
Document Validation
The insert
command adds support for thebypassDocumentValidation
option, which lets you bypassdocument validation wheninserting or updating documents in a collection with validationrules.
Transactions
insert
can be used inside multi-document transactions.
The collection must already exist. An insert operation thatwould result in the creation of a new collection are not allowed in atransaction.
Do not explicitly set the write concern for the operation if run ina transaction. To use write concern with transactions, seeTransactions and Write Concern.
Important
In most cases, multi-document transaction incurs a greaterperformance cost over single document writes, and theavailability of multi-document transactions should not be areplacement for effective schema design. For many scenarios, thedenormalized data model (embedded documents and arrays) will continue to be optimal for yourdata and use cases. That is, for many scenarios, modeling your dataappropriately will minimize the need for multi-documenttransactions.
For additional transactions usage considerations(such as runtime limit and oplog size limit), see alsoProduction Considerations.
Examples
Insert a Single Document
Insert a document into the users
collection:
- db.runCommand(
- {
- insert: "users",
- documents: [ { _id: 1, user: "abc123", status: "A" } ]
- }
- )
The returned document shows that the command successfully inserted adocument. See Output for details.
- { "ok" : 1, "n" : 1 }
Bulk Insert
Insert three documents into the users
collection:
- 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 thethree documents. See Output for details.
- { "ok" : 1, "n" : 3 }
Output
The returned document contains a subset of the following fields:
insert.
writeErrors
- An array of documents that contains information regarding any errorencountered during the insert operation. The
writeErrors
array contains an error document foreach insert that errors.
Each error document contains the following fields:
insert.writeErrors.
index
An integer that identifies the document in the
documents
array, which uses a zero-based index.An integer value identifying the error.
- A description of the error.
insert.
writeConcernError
Document that describe error related to write concern and containsthe field:
The following is an example document returned for a successfulinsert
of a single document:
- { ok: 1, n: 1 }
The following is an example document returned for aninsert
of two documents that successfully inserted onedocument but encountered an error with the other document:
- {
- "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 }"
- }
- ]
- }