MongoDB

How to insert document in MongoDB

MongoDB is an open source widely used database management system that follows the structure of NoSQL databases. MongoDB has its own querying language (MongoDB Query Language) to retrieve and insert data in MongoDB-based databases. Several methods are associated with different functions of MongoDB; for insertion, insert() method is used in MongoDB supported databases. The insert() operation has several other extensions that are also used to insert documents but with several properties, like the insertOne() method is used to insert a single document in a Mongo Collection.

In this article, we have provided a detailed guide to understand insert method in MongoDB:

How insert method works in MongoDB

The insert document functionality of MongoDB comprises of following three methods and the working mechanism of insert operation depends on the following methods:

Insert One document: This method allows you to insert only one document in a collection at a time. The syntax of this specific method is given below:

db.collection-name.insertOne({document})

The “collection-name” in the syntax is user defined.

Insert Many Documents: If multiple insertions of documents are required in a single collection, then you can use the Insert Many method.

To insert multiple documents, you have to follow the syntax given below:

db.collection-name.insertMany([{document1},{document2},{document3}])

Insert Multiple or One Document : The insert operation of MongoDB allows you to insert multiple or one document in a single method. The syntax for this combo method is given below:

To insert a single document: The syntax written below will assist you in inserting a single document.

db.collection-name.insert({document})

To insert multiple documents: The same insert() method can be used to add multiple documents to your Mongo collection by using the syntax given below:

db.collection-name.insert([{document1},{document2},{document3}])

Note: The parenthesis in the syntaxes are mandatory to follow, otherwise you may encounter wrong insertions.

How to use Insert method in MongoDB

This section comprises of several examples that demonstrate the application of each insert method in detail. Before getting into examples, we are using “linuxhint” as a database name and collection name will be changed in each example.

Example 1: Using insertOne() method

The query mentioned below will exercise the usage of the insertOne() method of MongoDB. It will add only one document to the “staff_info” collection.

> db.staff_info.insertOne({name: "Alen", designation: "Manager", experience: "3years"})

Graphical user interface, text, website Description automatically generated with medium confidence

Example 2: Using insertMany() method

This example illustrates the usage of insertMany() method by inserting multiple documents in a collection. For instance, the Mongo query written below will insert multiple documents in the “cars” collection using insertMany() method.

Note: In this example, the collection name is “cars“:

> db.cars.insertMany([

{Make: "BMW", Model: "2015", Price: "$100k"},

{Make: "Mercedes-Benz", Model: "2021", Price: "$150k"},

{Make: "TOYOTA", Model: "2021", Price: "$85k"},

])

Text Description automatically generated

The “true” message in the “acknowledged” part shows that the data is inserted successfully. The “insertedIds” displays the unique id assigned to each inserted document.

Example 3: Using insert() method

This example comprises of two parts:

Adding a Single Document: The query will show you to insert a single document using this method. We have used “appliances” as a collection in this example.

> db.appliances.insert({Cat: "Air-Conditioner", Qty: 100, Price: "$300K", Expiry: "2030"})

The output also displays a message that only one document is inserted.

Adding Multiple Documents: You can also add multiple documents by the same method; The below mentioned query assist to do so:

> db.appliances.insert([

{Cat: "Refrigerator", Qty: 30, Price: "$75k", Expiry: "2030"},

{Cat: "LED’s", Qty: 50, Price: "$60k", Expiry: "2030"},

{Cat: "Laptops", Qty: 70, Price: "$50k", Expiry: "2025"}

])

The above command contains three documents and after execution, the output also confirms the insertion of “3” documents.

Conclusion

MongoDB provides an extensive list of methods and operators that can be used to process data in databases. For insertion, MongoDB supports insert document functionality that consists of three methods. In this post, we have provided a sequential guide to insert documents in a collection of MongoDB databases. The three methods include: “insertOne(), insertMany(), and insert()” that are used to insert single, many, and “single or many” documents respectively. Among these, the “insert()” method is used the most because it has the dual functionality of adding many as well as single documents.

About the author

Adnan Shabbir