- Reference >
- Database Commands >
- Query and Write Operation Commands >
- insert
insert¶
On this page
Definition¶
-
insert
¶ The
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:
The
insert
command takes the following fields: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 theinserts
array. Iffalse
, then when an insert of a document fails, continue to insert the remaining documents. Defaults totrue
.writeConcern
document Optional. A document that expresses the write concern of the
insert
command. Omit to use the default write concern.Do not explicitly set the write concern for the operation if run in a transaction. To use write concern with transactions, see Transactions and Write Concern.
bypassDocumentValidation
boolean Optional. Enables
insert
to bypass document validation during the operation. This lets you insert documents that do not meet the validation requirements.New in version 3.2.
comment
any Optional. A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:
- mongod log messages, in the
attr.command.cursor.comment
field. - Database profiler output, in the
command.comment
field. currentOp
output, in thecommand.comment
field.
A comment can be any valid BSON type (string, integer, object, array, etc).
New in version 4.4.
Returns: A document that contains the status of the operation. See Output for details. - mongod log messages, in the
Behavior¶
Size Limit¶
The total size of all the documents
array elements must be less
than or equal to the maximum BSON document size
.
The total number of documents in the documents
array must be less
than or equal to the maximum bulk size
.
Document Validation¶
The insert
command adds support for the
bypassDocumentValidation
option, which lets you bypass
document validation when
inserting or updating documents in a collection with validation
rules.
Transactions¶
insert
can be used inside multi-document transactions.
Important
In most cases, multi-document transaction incurs a greater performance cost over single document writes, and the availability of multi-document transactions should not be a replacement for effective schema design. For many scenarios, the denormalized data model (embedded documents and arrays) will continue to be optimal for your data and use cases. That is, for many scenarios, modeling your data appropriately will minimize the need for multi-document transactions.
For additional transactions usage considerations (such as runtime limit and oplog size limit), see also Production Considerations.
Collection Creation in Transactions¶
Starting in MongoDB 4.4 with feature compatibility version
(fcv) "4.4"
, you can create collections and indexes
inside a multi-document transaction if the transaction is
not a cross-shard write transaction.
As such, for feature compatibility version (fcv)
"4.4"
and greater, if you specify an insert on a non-existing
collection in a transaction, the collection is implicitly created.
If the feature compatibility version (fcv) is
"4.2"
or less, the operation must be against an existing
collection.
Write Concerns and Transactions¶
Do not explicitly set the write concern for the operation if run in a transaction. To use write concern with transactions, see Transactions and Write Concern.
Examples¶
Output¶
The returned document contains a subset of the following fields:
-
insert.
ok
¶ The status of the command.
-
insert.
n
¶ The number of documents inserted.
-
insert.
writeErrors
¶ An array of documents that contains information regarding any error encountered during the insert operation. The
writeErrors
array contains an error document for each 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.
-
insert.writeErrors.
code
¶ An integer value identifying the error.
-
insert.writeErrors.
errmsg
¶ A description of the error.
-
-
insert.
writeConcernError
¶ Document that describe error related to write concern and contains the field:
-
insert.writeConcernError.
code
¶ An integer value identifying the cause of the write concern error.
-
insert.writeConcernError.
errmsg
¶ A description of the cause of the write concern error.
-
insert.writeConcernError.errInfo.
writeConcern
¶ New in version 4.4.
The write concern object used for the corresponding operation. For information on write concern object fields, see Write Concern Specification.
The write concern object may also contain the following field, indicating the source of the write concern:
-
insert.writeConcernError.errInfo.writeConcern.
provenance
¶ A string value indicating where the write concern originated (known as write concern
provenance
). The following table shows the possible values for this field and their significance:Provenance Description clientSupplied
The write concern was specified in the application. customDefault
The write concern originated from a custom defined default value. See setDefaultRWConcern
.getLastErrorDefaults
The write concern originated from the replica set’s settings.getLastErrorDefaults
field.implicitDefault
The write concern originated from the server in absence of all other write concern specifications.
-
-
The following is an example document returned for a successful
insert
of a single document:
The following is an example document returned for an
insert
of two documents that successfully inserted one
document but encountered an error with the other document: