The insertMany() function is used to insert multiple documents into a collection. It accepts an array of documents to insert into the collection.
Installation of mongoose module:
- You can visit the link Install mongoose module. You can install this package by using this command.
npm install mongoose
- After installing mongoose module, you can check your mongoose version in command prompt using the command.
npm version mongoose
- After that, you can just create a folder and add a file for example index.js, To run this file you need to run the following command.
node index.js
Filename: index.js
const mongoose = require('mongoose'); // Database connection useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true}); // User model const User = mongoose.model('User', { name: { type: String }, age: { type: Number } }); // Function call User.insertMany([ { name: 'Gourav', age: 20}, { name: 'Kartik', age: 20}, { name: 'Niharika', age: 20} ]).then(function(){ console.log("Data inserted") // Success }).catch(function(error){ console.log(error) // Failure }); |
chevron_right
filter_none
Steps to run the program:
- The project structure will look like this:

- Make sure you have install mongoose module using following command:
npm install mongoose
- Run index.js file using below command:
node index.js

- After running above command, your can see the data is inserted into the database. You can use any GUI tool or terminal to see the database, like I have used Robo3T GUI tool as shown below:

So this is how you can use mongoose insertMany() function to insert multiple documents into the collection in MongoDB and Node.js.
Recommended Posts:
- Mongoose | updateMany() Function
- Mongoose | findOneAndDelete() Function
- Mongoose | findByIdAndRemove() Function
- Mongoose | deleteOne() Function
- Mongoose | deleteMany() Function
- Mongoose | findByIdAndDelete() Function
- Mongoose | findByIdAndUpdate() Function
- Mongoose | findById() Function
- Mongoose | find() Function
- Mongoose | findOne() Function
- Mongoose | findOneAndRemove() Function
- Mongoose | findOneAndReplace() Function
- Mongoose | exists() Function
- Mongoose | where() Function
- Mongoose | replaceOne() Function
- Mongoose | updateOne() Function
- Mongoose | remove() Function
- Mongoose | update() Function
- Mongoose | findOneAndUpdate() Function
- Mongoose | save() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

