Menu Close

How to Insert Documents in MongoDB Collection

How to insert documents in mongodb

In this MongoDB article, we are about to learn how to insert documents in MongoDB collection with the help of the MongoDB compass application and MongoDB Mongosh command line tool.MongoDB provides two MongoDB collection methods to insert single or multiple documents in the MongoDB collection.

You must have a database and collection to insert documents into the MongoDB collection. You can see below the MongoDB tutorial to create a database and collection.

See Also:
➡️How to Create Database in the MongoDB
➡️How to Create Collection in the MongoDB

What is a Document in the MongoDB?

MongoDB documents represent a record just like a record in SQL Databases like MySQL, PostgreSQL, etc.MongoDB documents are similar to JSON-like objects which store information in the form of key values. The Value of the key may include another document, array, and array of documents.

You can see below, that I have defined a sample of the MongoDB document.

{
  "first_name": "Programming",
  "last_name": "Funda",
  "age": 4,
  "tutorials": ["Python", "MongoDB", "Pandas", "MySQL"]	
}

In the above sample MongoDB document, There are four keys and their values.

Now Let’s see the process of inserting documents into the MongoDB collection.

Insert Documents in MongoDB Collection:

First, we will see document insertion with the help of the MongoDB compass application. If you don’t want to insert documents with the help of the Mongosh command then you can use the MongoDB compass GUI ( Graphical User Interface ) application. It is a desktop application to manages all the operations in the MongoDB database.

Let’s insert documents in MongoDB.

I have already created the MongoDB database and collection in my previous tutorials. You can get that tutorial from below.

See Also:
➡️How to Create Database in the MongoDB
➡️How to Create Collection in the MongoDB

Let’s start!

Insert a Single Document in MongoDB using the MongoDB compass

Here, I am going to insert documents into the employee collection of the ProgrammingFunda database.

  • Open MongoDB Compass Application.
  • Click on the Connect button to connect to the MongoDB database.
Insert Documents in MongoDB Collection
  • Click on Collection under Database. Here, I am selecting the employee collection under the ProgrammingFunda database.
Insert Documents in MongoDB Collection
  • Click on Insert document under the Add Data section.
Insert Documents in MongoDB Collection
  • Now, You will see a window like this.
Insert Documents in MongoDB Collection
  • Prepare the document object that you want to store in the collection.
  • Paste your document object into the Insert Document window and click on the Insert Button. Here, I have prepared a single document to be inserted.
{
"emp_name": "Vishvajit",
"emp_age": 20,
"emp_email": "[email protected]"
}
Insert Documents in MongoDB Collection
  • After inserting the document, your documents will be shown in the collection, as you can below.
Insert Documents in MongoDB Collection

Insert Multiple Documents in MongoDB using MongoDB Compass

To insert multiple documents, you need to follow the above steps completely but you have to put all your document objects into an array and paste them into the Insert Document window just like below, and finally click on Insert.

[{
"emp_name": "Vishvajit",
"emp_age": 20,
"emp_email": "[email protected]"
},
{
"emp_name": "Hasrhita",
"emp_age": 23,
"emp_email": "[email protected]"
},
{
"emp_name": "John",
"emp_age": 30,
"emp_email": "[email protected]"
}
]
Insert Documents in MongoDB Collection

After that you will see all your documents of collection.

Insert Documents in MongoDB Collection

Important!

Note:- If your document does not have any _id filed, Then MongoDB will add an _id field in each document and assign a unique value to each of them as you can see in the above picture.

This is how you can insert single or multiple documents into the MongoDB collection.

Now let’s see the process of inserting single or multiple documents into MongoDB with the help of the Mongosh command.

What is Mongosh?

MOngosh is a command line tool that is used to manage all the MongoDB operations with the help of a command line. You can perform almost all the operations that you can perform in the MongoDB compass application.

If you are developing, Then definitely you must try this tool.

Let’s start inserting the document.

Insert a Single Document in MongoDB using Mongosh

Mongosh provides a straightforward method called insertOne(). This method is responsible for inserting a single document into a collection.

Now Let’s see how we can insert a single document.

  • Open the command prompt in your system.
  • Type mongosh to switch into a mongosh shell.
Insert Documents in MongoDB Collection
  • After that, Switch to the database in which when you want to insert a document. As you can in the below picture, I am switching to the ProgrammingFunda database using the ‘use ProgrammingFunda‘ command. Here, ProgrammingFunda represents the name of the database.
Insert Documents in MongoDB Collection
  • After switching to the database, You can display all the available collections with the help of the show collections command.
Insert Documents in MongoDB Collection
  • As you can see in the above picture, I have already created a student collection, Now will insert a document into it.
  • To insert a single document, use the below command.
db.student.insertOne({ "first_name": "Programming", "last_name": "Funda", "age": 4 })

In the above command, { "first_name": "Programming", "last_name": "Funda", "age": 4 } represents a single document.

  • After the successful execution of the above command, you will see the output below, Which means you have successfully inserted the document into your MongoDB collection.
Insert Documents in MongoDB Collection
  • To display the inserted document, you can use the find() collection method. This is how.
Insert Documents in MongoDB Collection

This is the way to insert a single document into the collection with the help of the insertOne() method.

Insert Multiple Documents in MongoDB using Mongosh

To insert Multiple documents in MongoDB, Follow the same process but instead of using insertOne() use the insert insertMany() method and pass an array of documents into insertMany() method.

You can see below a picture of how I passed the array of documents into the insertMany() method to insert multiple documents.

After inserting documents you can use the db.collection_name.find() method to display all the documents of the collection.

Let me display all the documents of the student collection by using db.student.find().

This is how you can use the insertOne() and insertMany() methods to insert single or multiple documents into the MongoDB collection.


FAQs

What is a document in MongoDB?

Ans: A document is a single record that stores information in the form of a key: value pair. Mongodb document is the same as a JSON-like object. The document is always stored in a MongoDB Collection. A single document is the same as a single record in an SQL database.

What is collection in MongoDB?

Ans: Collection in MongoDB is a kind of table that is used to store documents.MongoDB stores its data in the collection. MongoDB collection is almost the same as RDBM tables.

What is the difference between insertOne() and insertMany() methods?

Ans: Both methods are mongoDB collection methods which means these two method will only apply on top of the collection.
insertOne() method is used to insert only a single record into a table whereas insertMany() method is used to insert multiple documents into collection.

Do MongoDB Databases Have Tables?

Ans: Instead of storing data in tables, MongoDB database stores its data in collections. A MongoDB database may have multiple collections. Collections in MongoDB database are almost equivalent to tables in SQL Databases.


Conclusion

In Conclusion, We have seen the complete process of inserting documents in MongoDB with the help of the MongoDB compass application as well as using the MongoDB mongosh command.

As a developer or database administrator you should use both tools MongoDB compass and Mongosh command line tool. You can perform almost all operations in MongoDB by using these tools.

If you want to insert only one record then should always use the insertOne() method and if you want to insert more than one document then you go with the insertMany() method.

If you found this article helpful, please share and keep visiting for further Mongodb tutorials.

Thanks for your time…

How to Create a Collection in MongoDB

Related Posts