Usage of Arrays for Insertion of Multiple Documents on MongoDB.
In our last tutorial on the MongoDB Beginners Guide Training series, we explored about Create and Insert Database in MongoDB.
We also learned about insert () command that is used for insertion of one document at a time into the mongo collection.
In this tutorial, we will see how we could use the insert () command for insertion of multiple documents at a time.
Example:
Here we will see the step-by-step guide for using the insert () command.
Step 1: Firstly, we will create a variable of JavaScript which would be “myStaff“. The “myStaff” variable has an array of multiple documents.
Step 2: Secondly, we will put the records i.e. Field Names & Field Values into the variable i.e. “myStaff“.
Step 3: At the end, we will insert the array of multiple documents by using the insert () command.
The given command must be run on MongoDB to get the desired result.
Syntax:
var myStaff= [ { "staff_id": 01, "staff_member_name": "Jhon" }, { "staff_id": 02, "staff_member_name": "Marrie" }, { "staff_id": 03, "staff_member_name": "Obrien" }, ]; db.staff.insert(myStaff);
Press Enter.
Figure 1: In Mongo Shell
Figure 2: In Robo 3T
If this command runs successfully, then the following would be the output.
Output:
Figure 3: In Mongo Shell
Output:
Figure 4: In Robo 3T
The given output displays that we have inserted 3 documents into the mongo collection.
Table of Contents:
Printing in the format of JavaScript Object Notation
JavaScript Object Notation i.e. JSON is a format which is used to compile the information that has been stored in MongoDB into a standardized form which would make it more readable.
In our next Example, we will use JSON print to see the output of the above query in a more readable format.
To print the output in JSON format, the following query has to be run on cmd.
Syntax: db.staff.find().forEach(printjson)
Figure 5
Description:
- The function Each () would be attached to the find () function. It is done to mobilize the function to go through from each and every document present in the collection.
- The Function PrintJson is commanded for Each () function. This will display all the documents present in the collection in a JSON Format.
If the above command runs without error, then the following would be the output.
Figure 6: In Mongo Shell
Figure 7: In Robo 3T
As we see, the output is readable and is in the JSON format.
Conclusion
These are the steps through which we can make multiple documents get stored in mongo collections by using arrays in the insert () function. If we put the whole document in a nutshell, the following are the pointers that we have covered.
These are:
- How could we use insert () command for insertion of multiple documents at a time?
- How to print the output in JSON format. The following query has to be run on cmd: db.staff.find().forEach(printjson).
- The function Each () with find () function and how this function goes through from each and every document present in the collection.
- The Function PrintJson with Each () function and how it displays all the documents present in the collection in a JSON Format.
We will explore the Usage of Update() and Delete() Document in MongoDB in our upcoming tutorial!!