MongoDB Insert Document - MongoDB

How to insert a document in MongoDB?

In this chapter, we will explain how to insert document in MongoDB collection.

insert() Method

To insert data into MongoDB collection, you can use MongoDB's insert() or save() method.


Basic syntax of insert() command is as follows


Here mycol is the collection name which we created in the previous chapter. If the collection does not exist in the database, MongoDB will create this collection and then inserts a document into it.

In the inserted document, if _id parameter is not specified, then MongoDB will assign a unique ObjectId for this document.

_id is 12 bytes hexadecimal number unique for every document in a collection. 12 bytes are divided as follows

To insert multiple documents in a single query, pass an array of documents in insert() command.


To insert the document, use can also be used. If _id is not specified in the document, then save() method will work same as insert() method. If _id is specified, then it will replace whole data of document containing _id as specified in save() method.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

MongoDB Topics