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:
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:
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.
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:
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.
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“:
{Make: "BMW", Model: "2015", Price: "$100k"},
{Make: "Mercedes-Benz", Model: "2021", Price: "$150k"},
{Make: "TOYOTA", Model: "2021", Price: "$85k"},
])
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.
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:
{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.