Javatpoint Logo
Javatpoint Logo

MongoDB insert documents

In MongoDB, the db.collection.insert() method is used to add or insert new documents into a collection in your database.

Upsert

There are also two methods "db.collection.update()" method and "db.collection.save()" method used for the same purpose. These methods add new documents through an operation called upsert.

Upsert is an operation that performs either an update of existing document or an insert of new document if the document to modify does not exist.

Syntax

Let?s take an example to demonstrate how to insert a document into a collection. In this example we insert a document into a collection named javatpoint. This operation will automatically create a collection if the collection does not currently exist.

Example

After the successful insertion of the document, the operation will return a WriteResult object with its status.

Output:

WriteResult({ "nInserted" : 1 })

Here the nInserted field specifies the number of documents inserted. If an error is occurred then the WriteResult will specify the error information.

Check the inserted documents

If the insertion is successful, you can view the inserted document by the following query.

You will get the inserted document in return.

Output:

{ "_id" : ObjectId("56482d3e27e53d2dbc93cef8"), "course" : "java", "details" : 
{ "duration" : "6 months", "Trainer" : "Sonoo jaiswal" }, "Batch" : 
[ {"size" : "Small", "qty" : 15 }, { "size" : "Medium", "qty" : 25 } ],
 "category" : "Programming language" }

Note: Here, the ObjectId value is generated by MongoDB itself. It may differ from the one shown.

MongoDB insert multiple documents

If you want to insert multiple documents in a collection, you have to pass an array of documents to the db.collection.insert() method.

Create an array of documents

Define a variable named Allcourses that hold an array of documents to insert.

Inserts the documents

Pass this Allcourses array to the db.collection.insert() method to perform a bulk insert.

After the successful insertion of the documents, this will return a BulkWriteResult object with the status.

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

Note: Here the nInserted field specifies the number of documents inserted. In the case of any error during the operation, the BulkWriteResult will specify that error.

You can check the inserted documents by using the following query:

Insert multiple documents with Bulk

In its latest version of MongoDB (MongoDB 2.6) provides a Bulk() API that can be used to perform multiple write operations in bulk.

You should follow these steps to insert a group of documents into a MongoDB collection.

Initialize a bulk operation builder

First initialize a bulk operation builder for the collection javatpoint.

This operation returns an unorder operations builder which maintains a list of operations to perform .

Add insert operations to the bulk object

Execute the bulk operation

Call the execute() method on the bulk object to execute the operations in the list.

After the successful insertion of the documents, this method will return a BulkWriteResult object with its status.

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

Here the nInserted field specifies the number of documents inserted. In the case of any error during the operation, the BulkWriteResult will specify that error.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA