Docs Menu

Docs HomeMongoDB Manual


On this page

  • Definition
  • Behavior
  • Examples
  • Output

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.


In mongosh, this command can also be run through the db.collection.insertOne() and db.collection.insertMany() helper methods.

Helper methods are convenient for mongosh users, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.

The command has the following syntax:

insert: <collection>,
documents: [ <document>, <document>, <document>, ... ],
ordered: <boolean>,
writeConcern: { <write concern> },
bypassDocumentValidation: <boolean>,
comment: <any>

The insert command takes the following fields:

The name of the target collection.
An array of one or more documents to insert into the named collection.
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.

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.


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.


Optional. A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:

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.

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.

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.

insert can be used inside multi-document transactions.


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.

Starting in MongoDB 4.4, you can create collections and indexes inside a multi-document transaction if the transaction is not a cross-shard write transaction.

Specifically, in MongoDB 4.4 and greater, if you specify an insert on a non-existing collection in a transaction, the collection is implicitly created.

In MongoDB 4.4 and earlier, the operation must be run on an existing collection.


See also:

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.

Even if you encounter a server error during an insert, some documents may have been inserted.

After a successful insert, the system returns insert.n, the number of documents inserted into the collection. If the insert operation is interrupted by a replica set state change, the system may continue inserting documents. As a result, insert.n may report fewer documents than actually inserted.

Insert a document into the users collection:

insert: "users",
documents: [ { _id: 1, user: "abc123", status: "A" } ]

The returned document shows that the command successfully inserted a document. See Output for details.

{ "ok" : 1, "n" : 1 }

Insert three documents into the users collection:

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 Output for details.

{ "ok" : 1, "n" : 3 }

If schema validation validationActions are set to error, inserts to a collection return errors for documents that violate the schema validation rules. To insert documents which would violate these rules set bypassDocumentValidation: true.

Create the user collection with a validation rule on the status fields.

The validation rule validates that the status must be "Unknown" or "Incomplete":

db.createCollection("users", {
status: {
$in: [ "Unknown", "Incomplete" ]

Attempt to insert a document which violates the validation rule:

insert: "users",
documents: [ {user: "123", status: "Active" } ]

The insert returns a write error message:

n: 0,
writeErrors: [
index: 0,
code: 121,
errInfo: {
failingDocumentId: ObjectId('6197a7f2d84e85d1cc90d270'),
details: {
operatorName: '$in',
specifiedAs: { status: { '$in': [Array] } },
reason: 'no matching value found in array',
consideredValue: 'Active'
errmsg: 'Document failed validation'
ok: 1

Set bypassDocumentValidation : true and rerun the insert:

insert: "users",
documents: [ {user: "123", status: "Active" } ],
bypassDocumentValidation: true

The operation succeeds.

To check for documents that violate schema validation rules, use the validate command.

The returned document contains a subset of the following fields:


The status of the command.


The number of documents inserted.


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:


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.


Document that describe error related to write concern and contains the field:


An integer value identifying the cause of the write concern error.


A description of the cause of the write concern error.


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:


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:

The write concern was specified in the application.
The write concern originated from a custom defined default value. See setDefaultRWConcern.
The write concern originated from the replica set's settings.getLastErrorDefaults field.
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:

{ ok: 1, n: 1 }

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:

"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 }"
←  getMoreresetError →
Share Feedback
© 2022 MongoDB, Inc.


  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.