MongoDB Insert Documents – MongoDB CRUD Operations Part 1

In this chapter, we will learn about MongoDB insert documents – how to insert documents in the collections. There are following ways from which we can insert documents in the collections:

  • insert () Method
  • insertOne() Method
  • insertMany () Method

Let’s see these methods in brief.

MongoDB Insert Documents


  • insert () Method – Inserting the documents

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

Syntax:

db.COLLECTION_NAME.insert(document)  

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 ‘TutorialsJar’ in the database ‘mohit’.

Note: This operation will automatically create a collection if the collection does not currently exist.

Example

db.TutorialsJar.insert(  
{  
   tutorial: "mongodb",  
   details: {  
              title: "MongoDB Insert documents – MongoDB CRUD Operations Part 1",  
              Author: "Mohit Arora"  
            }
}
)

MongoDB Insert Documents

After the successful insertion of the document, method returns a WriteResult object with its status. As you can see below message after inserting the document:

WriteResult({ “nInserted” : 1 })

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

Note: You can see that there was no existing collection named as ‘TutorialsJar’. Once, we inserted the document, it automatically created the ‘TutorialsJar’ collections.

Now let’s check if the document is inserted in the required collection by the following query.

Syntax:

db.TutorialsJar.find().pretty();

Output:

MongoDB Insert Documents 1

Here, you can see one additional field (_id) in the collection. In the inserted document, if we don’t specify the _id parameter, then MongoDB assigns a unique ObjectId for this document and adds the id field.

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

_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id,    3 bytes incrementer)

2) InsertOne() Method – Inserting a Single Document

This method is used to insert single document into a MongoDB collection. This is similar to insert() method but the only difference is that it can insert only single document into a given collection. Also, insertOne() is not compatible with db.collection.explain(), so use insert() instead.

Syntax:

db.collection.insertOne();

Here, I am inserting document in the ‘counting’ collection:

db.counting.insertOne({ a: "1",b:”2”,c:”3”})

Output:

MongoDB Insert Documents 2

3) insertMany() method – MongoDB insert multiple documents

Similarly, you can insert multiple documents in a single query, you can pass an array of documents in insertMany() command.

db.info.insertMany([{tutorial: “MongoDB”,details:{title:”MongoDB Insert Documents”,

author:”Mohit Arora”}},{tutorial: “MongoDB”,details:{title: ”MongoDB Delete Documents”,

author:”Mohit”}}]);

Below is the snapshot of above command in better way:

MongoDB Insert Documents 3

Output:

MongoDB Insert Documents 4

Also, note that insertMany() is not compatible with db.collection.explain(). We can use insert() instead.

That’s it in this MongoDB insert documents – MongoDB CRUD Operations Part 1. In the next chapter, we will read about MongoDB update documents (CRUD Operations Part 2). If you have any queries, please comment in the comment section below. Also, do share your feedback 🙂

Mohit Arora
Follow me