OPTIONS

insert

Definition

insert

New in version 2.6.

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:

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

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 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 write concern of the insert command. Omit to use the default 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.

Returns:A document that contains the status of the operation. See Output for details.

Behavior

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.

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 a document. 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 the three documents. See Output for details.

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

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.

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 }"
      }
   ]
}
←   find update  →

Was this page helpful?

Yes No

Thank you for your feedback!

We're sorry! You can Report a Problem to help us improve this page.