insertMany Function in MongoDB and Nodejs: Ultimate Guide

The insertMany function is the most basic function that we come across when using MongoDB. It allows us to insert one or more documents all together 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 as an argument.

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 behaviour, this function will insert documents in the order we passed. You may set the ordered option as false if you do not want the function to follow the specified order.

This beginner’s guide on the insertMany function shall give you a walkthrough with examples of how to use this function in the Mongo Shell and in a Node.js application. So, let us get started.

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
})

Also Read: NodeJS MongoDB Insert

Using the MongoDB insertMany Function in Mongo Shell

Mongo Shell is the fastest way of interacting with the MongoDB database, let’s see an example to demonstrate the use of this method in Mongo Shell.

  • Start up your MongoDB server.
  • Pick the database you want to use and navigate to it.
show dbs
use droneSpree
  • Let us take a look at all our existing documents in a particular collection where we will insert documents later using insertMany function.
> 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, let’s insert documents using 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"
...                 ]
...         }
... }
… ])

Here we have passed an array containing two documents in the insertMany function to insert it in a given collection.

  • Let’s see what our collection looks like now.
> 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"
                 ]
         }
}

See, we got the output containing all the documents, including those we just inserted, and also has automatically generated the _id for every newly inserted document.

Using the MongoDB insertMany Function in Node.js

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

  • Let us create or insert a few documents in our collection via Node.js. Assume we have written the below 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 file by executing the below command in a terminal.
node insertManyDoc.js

Note: Make sure that the MongoDB server is up and running.

  • Switch to Mongo Shell and print all the documents to see if the specified documents have been inserted or not.
> 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 Node.js. The insertMany function is used to insert documents in MongoDB database collections, as the name suggests, it can insert multiple documents at one go. It takes an array as an argument containing many documents to be inserted into a given MongoDB database collection. We hope this tutorial helps you to understand the insertMany function in MongoDB.

Reference

https://www.mongodb.com/docs/manual/reference/method/db.collection.insertMany/

Aneesha S
Aneesha S
Articles: 174