insertMany Function in MongoDB and Nodejs: Ultimate Guide

InsertMany Function Featured Image

Today’s MongoDB tutorial walks you through the insertMany function in MongoDB as well as Nodejs.

Today, the insertMany function of MongoDB is of our interest. This is the most basic of the functions that we come across when using MongoDB. It allows us to insert one or more documents altogether at a time. Moreover, you can even create a collection and add multiple documents using the insertMany function. This function accepts an array of documents inside it.

We do not need to specify an ’_id’ property for the document we insert, as the insertMany function inserts it automatically for us. As a default behavior, the function will insert documents in the order we passed. If you do not want the function to follow the specified order, you may simply set the ordered option as false.

This beginner’s guide on the insertMany function shall give you a walkthrough with examples on how to use this insert operation function in the mongo shell and in a Nodejs application. So, let us get started.

The MongoDB insertMany Function Syntax:

Let us take a quick look at the insertMany function in MongoDB:

db.Collection_name.insertMany(
[document 1, document 2, …],
{
    writeConcern: document,
    ordered: boolean
})

Using the insertMany Function in MongoDB and Nodejs

From here on, I will show you how to use the insertMany function in MongoDB and Nodejs.

Using the insertMany Function in Mongo Shell

In this example, we will use the insertMany function in the mongo shell.

  • Start up your MongoDB server
  • Pick the database you want to use and navigate into it:
show dbs
use droneSpree
  • Let us take a look at all our existing documents in our collections:
> db.drones.find({}).pretty()

{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe1e"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Darkpact Y-copter X92",
        "price" : 79500,
        "weight" : "15 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Delivery",
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe1f"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Rizolli Debra TR-7",
        "price" : 48500,
        "weight" : "11 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe20"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Crohn Firebat O65",
        "price" : 12500,
        "weight" : "5 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
  • Now, we will insert more multiple documents into our collection using the insertMany function:
> db.drones.insertMany([{
...         "utility" : [
...                 "Photography",
...                 "Combat",
...                 "Rescue",
...                 "Construction"
...         ],
...         "onSale" : false,
...         "name" : "Genevieve Portman’s Thunderfin",
...         "price" : 19500,
...         "weight" : "12 kilograms",
...         "additionalDetails" : {
...                 "material" : "lithium",
...                 "moreUses" : [
...                         "Videography",
...                         "Recreation"
...                 ]
...         }
... },
… {
...         "utility" : [
...                 "Photography",
...                 "Combat",
...                 "Rescue",
...                 "Construction"
...         ],
...         "onSale" : false,
...         "name" : "Berkeley Eagle 67",
...         "price" : 89500,
...         "weight" : "22 kilograms",
...         "additionalDetails" : {
...                 "material" : "glass fiber",
...                 "moreUses" : [
...                         "Videography",
...                         "Recreation"
...                 ]
...         }
... }
… ])
  • Let us look at what our collection now looks like:
> db.drones.find({}).pretty()

{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe1e"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Darkpact Y-copter X92",
        "price" : 79500,
        "weight" : "15 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Delivery",
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe1f"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Rizolli Debra TR-7",
        "price" : 48500,
        "weight" : "11 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe20"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Crohn Firebat O65",
        "price" : 12500,
        "weight" : "5 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba1t76589"),
         "utility" : [
                 "Photography",
                 "Combat",
                 "Rescue",
                 "Construction"
         ],
         "onSale" : false,
         "name" : "Genevieve Portman’s Thunderfin",
         "price" : 19500,
         "weight" : "12 kilograms",
         "additionalDetails" : {
                 "material" : "lithium",
                 "moreUses" : [
                         "Videography",
                         "Recreation"
                 ]
         }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba178878"),
         "utility" : [
                 "Photography",
                 "Combat",
                 "Rescue",
                 "Construction"
         ],
         "onSale" : false,
         "name" : "Berkeley Eagle 67",
         "price" : 89500,
         "weight" : "22 kilograms",
         "additionalDetails" : {
                 "material" : "glass fiber",
                 "moreUses" : [
                         "Videography",
                         "Recreation"
                 ]
         }
}

You can also notice that the insertMany function has automatically generated the ‘_id’ for us.

Using the MongoDB insertMany Function in Nodejs

In this example, we will be using the MongoDB insertMany function in a Nodejs application. For this, you will need a Nodejs application.

  • Let us create or insert a few documents in our collection via Nodejs. Assume we have written this code in a file named insertManyDoc.js:
await db.drones.insertMany([{
        "utility" : [
                "Security",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Sloan Fray Flier 876",
        "price" : 8500,
        "weight" : "2 kilograms",
        "additionalDetails" : {
                "material" : "polystyrene",
                "moreUses" : [
                        "Land Inspection",
                        "Photography"
                ]
        }
},
{
        "utility" : [
                "Security",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Trex3 Flier 003",
        "price" : 6700,
        "weight" : "5 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Water Inspection",
                        "Videography"
                ]
        }
}
])
  • Now, run insertManyDoc.js. Also, make sure your MongoDB server is up and running:
node insertManyDoc.js
  • Switch to your mongo shell and print all the documents to see if the specified documents have been inserted:
> db.drones.find({}).pretty()

{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe1e"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Darkpact Y-copter X92",
        "price" : 79500,
        "weight" : "15 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Delivery",
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe1f"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Rizolli Debra TR-7",
        "price" : 48500,
        "weight" : "11 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe20"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Crohn Firebat O65",
        "price" : 12500,
        "weight" : "5 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba1t76589"),
         "utility" : [
                 "Photography",
                 "Combat",
                 "Rescue",
                 "Construction"
         ],
         "onSale" : false,
         "name" : "Genevieve Portman’s Thunderfin",
         "price" : 19500,
         "weight" : "12 kilograms",
         "additionalDetails" : {
                 "material" : "lithium",
                 "moreUses" : [
                         "Videography",
                         "Recreation"
                 ]
         }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba178878"),
         "utility" : [
                 "Photography",
                 "Combat",
                 "Rescue",
                 "Construction"
         ],
         "onSale" : false,
         "name" : "Berkeley Eagle 67",
         "price" : 89500,
         "weight" : "22 kilograms",
         "additionalDetails" : {
                 "material" : "glass fiber",
                 "moreUses" : [
                         "Videography",
                         "Recreation"
                 ]
         }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba7432e"),
        "utility" : [
                "Security",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Sloan Fray Flier 876",
        "price" : 8500,
        "weight" : "2 kilograms",
        "additionalDetails" : {
                "material" : "polystyrene",
                "moreUses" : [
                        "Land Inspection",
                        "Photography"
                ]
        }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba1654r3b"),
        "utility" : [
                "Security",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Trex3 Flier 003",
        "price" : 6700,
        "weight" : "5 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Water Inspection",
                        "Videography"
                ]
        }
}

Great! The MongoDB insertMany function has inserted the specified documents.

Read More: Easy Guide to Use findOneAndUpdate in MongoDB Using Nodejs

Conclusion

In this article, we’ve learned how to use the insertMany function in the Mongo shell and in a Nodejs application.

Noteworthy References

MongoDB