Insert Multiple Documents in MongoDB Using Arrays

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated March 7, 2024

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.

Using arrays for an insert statement on MongoDB

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

use of insert () command In Mongo Shell

Figure 2: In Robo 3T

insert command in Robo 3T

If this command runs successfully, then the following would be the output.

Output:

Figure 3: In Mongo Shell

Field Names and Field Values in Mongo Shell

Output:

Figure 4: In Robo 3T

Field Name and Field Values in Robo 3T

The given output displays that we have inserted 3 documents into the mongo collection.

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

Printing in the format of JavaScript Object Notation

Description:

  1. 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.
  2. 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

Inserting Multiple docs In Mongo Shell

Figure 7: In Robo 3T

Printing in JSON format 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:

  1. How could we use insert () command for insertion of multiple documents at a time?
  2. How to print the output in JSON format. The following query has to be run on cmd: db.staff.find().forEach(printjson).
  3. The function Each () with find () function and how this function goes through from each and every document present in the collection.
  4. 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!!

PREV Tutorial | NEXT Tutorial

Was this helpful?

Thanks for your feedback!

Leave a Comment