The insertMany() function is used to insert multiple documents into a collection. It accepts an array of documents to insert into the collection. This function is efficient and can significantly speed up the process of adding multiple documents compared to inserting each document individually.
Syntax
Model.insertMany(docs, options, callback);Parameters: The Model.insertMany() method accepts three parameters:
- docs: An array of objects (documents) to insert into the collection.
- options (optional): An object with the following properties:
ordered(boolean):- If
true, MongoDB inserts the documents in order and stops at the first error. - If
false, it continues inserting even if some documents fail. - Default:
true
- If
rawResult(boolean):- If
true, returns the raw MongoDB result object. - Default:
false
- If
- callback (optional): A function that is executed once insertion is complete. If omitted, a Promise is returned.
Returns: The Model.insertMany() function returns a promise. The result contains an array of objects having details of documents inserted in the database.
The
insertMany()function is highly useful for adding multiple records in MongoDB.
Steps to Installation of Mongoose Module
Step 1: Initialise the Node.js app using the command below.
npm init -yStep 2: Install mongoose with the following command.
npm install mongooseStep 3: After installing the mongoose module, you can check your mongoose version in the command prompt using the command.
npm version mongooseProject Structure

The updated dependencies in package.json file will look like
"dependencies": {
"mongoose": "^8.13.2",
}Example: Below the code example for the insertMany() method
const mongoose = require('mongoose');
// Database connection
mongoose.connect(
'mongodb://127.0.0.1:27017/geeksforgeeks',
{
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
});
Run the application using the following command
node index.js
After running the above command, you 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 the Robo3T GUI tool as shown below
So this is how you can use the Mongoose insertMany() function to insert multiple documents into the collection in MongoDB and Node.