w3resource

MongoDB: db.collection.insert() method

db.collection.insert

The db.collection.insert() method is used to insert a new document in a collection.

Syntax:

db.collection.insert()

Parameters:

Name Description Required /
Optional
Type
document A document or array of documents to insert into the collection. Required document or array
writeConcern A document expressing the write concern. Omit to use the default write concern. Optional document
ordered If true, perform an ordered insert of the documents in the array, and if an error occurs with one of the documents, MongoDB will return without processing the remaining documents in the array.
If false, perform an unordered insert, and if an error occurs with one of the documents, continue processing the remaining documents in the array.
Defaults to true.
Optional boolean

Returns:

  • A WriteResult object for single inserts.
  • A BulkWriteResult object for bulk inserts.

Example: Insert a Document without Specifying an_idField

In the following example, the document passed to theinsert()method does not contain the_idfield:

db.invoice.insert( { inv_no: "I00001", inv_date: "10/10/2012" } );

Output:

> db.invoice.insert( { inv_no: "I00001", inv_date: "10/10/2012" } );
WriteResult({ "nInserted" : 1 })

During the insert, mongod will create the _id field and assign it a unique ObjectId value, as verified by the inserted document. To see the inserted document see the following statement.


> db.invoice.find();
{ "_id" : ObjectId("567554d2f61afaaed2aae48f"), "inv_no" : "I00001", "inv_date" : "10/10/2012" }

Example: Insert a Document Specifying an_idField

In the following example, the document passed to theinsert()method includes the_idfield. The value of_idmust be unique within the collection to avoid duplicate key error.

db.invoice.insert( { _id: 901,inv_no: "I00001", inv_date: "10/10/2012" } );

Output:

> db.invoice.insert( { _id: 901,inv_no: "I00001", inv_date: "10/10/2012" } );
WriteResult({ "nInserted" : 1 })

The operation inserts the following document in theproductscollection:

{ "_id" : 901, "inv_no" : "I00001", "inv_date" : "10/10/2012" }

Example: Insert Multiple Documents

The following example performs a bulk insert of three documents by passing an array of documents to the insert()method. The documents in the array do not need to have the same fields. For instance, the first document in the array has an_idfield and aunitfield. But the second and third documents do not contain an_idfield, mongodwill create the_idfield for the second and third documents during the insert:

db.orders.insert(
   [
     { _id: 15, ord_no: 2001, qty: 200, unit: "doz" },
     { ord_no: 2005, qty: 320 },
     { ord_no: 2008, qty: 250, rate:85 }
   ]
);

Output:

BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 3,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})

The operation inserted the following three documents:

{ "_id" : 15, "ord_no" : 2001, "qty" : 200, "unit" : "doz" }
{ "_id" : ObjectId("56755896f61afaaed2aae490"), "ord_no" : 2005, "qty" : 320 }
{ "_id" : ObjectId("56755896f61afaaed2aae491"), "ord_no" : 2008, "qty" : 250, "rate" : 85 }

Retrieve the restaurants data from here

Behavior

Safe Writes

The insert() method uses the insert command, which uses the default write concern. To specify a different write concern, include the write concern in the options parameter.

Create Collection

If the collection does not exist, then the insert() method will create the collection.

_id Field

If the document does not specify an _id field, then MongoDB will add the _id field and assign a unique ObjectId for the document before inserting. Most drivers create an ObjectId and insert the _id field, but the mongod will create and populate the _id if the driver or application does not.

If the document contains an _id field, the _id value must be unique within the collection to avoid duplicate key error.

Previous: db.collection.group() method
Next: db.collection.isCapped() method



Follow us on Facebook and Twitter for latest update.