How to Make Mongoose Multiple Collections using Node.js ?
Last Updated :
21 Jun, 2024
Creating multiple collections in MongoDB using Mongoose with Node.js involves defining different Mongoose schemas and models for each collection. Mongoose allows for a straightforward and schema-based approach to managing MongoDB collections. Here’s a step-by-step guide on how to create and manage multiple collections using Mongoose in a Node.js application.
To create a collection with Mongoose you have to create two necessary things:
- Schema: It is a document structure that contains the property with its types ( default value, validations, etc. when required) as a key-value pair.
- Model: It is a class created with the help of defined Schema and a MongoDB document is an instance of the Model. Therefore, it acts as an interface for the MongoDB database for creating, reading, updating, and deleting a document.
Steps to Setup Project
Step 1: Make a folder structure for the project.
mkdir myapp
Step 2: Navigate to the project directory
cd myapp
Step 3: Initialize the NodeJs project inside the myapp folder.
npm init -y
Step 4: Install the necessary packages/libraries in your project using the following commands.
npm install mongoose
Project Structure:

The updated dependencies in package.json file will look like:
"dependencies": {
"mongoose": "^8.4.3",
}
Step 1: Create a Folder and add ‘model.js’ and ‘main.js’ files into it
- model.js: It contains schemas and models for all the collections you want to create, and then we are exporting all the models created so that they can be imported into the file in which we will create our collections.
- main.js: It is the main server file here we have inserted data into a collection.
Step 2: Write down the following code in the model.js File
Node
// model.js
// Requiring module
const mongoose = require('mongoose');
// Course Modal Schema
const courseSchema = new mongoose.Schema({
_id: Number,
name: String,
category: String
});
// Student Modal Schema
const studentSchema = new mongoose.Schema({
name: String,
enroll: Number,
courseId: Number
});
// Teacher Modal Schema
const teacherSchema = new mongoose.Schema({
name: String,
teacher_id: Number,
courseId: Number
})
// Creating model objects
const Course = mongoose.model('course', courseSchema);
const Student = mongoose.model('student', studentSchema);
const Teacher = mongoose.model('teacher', teacherSchema);
// Exporting our model objects
module.exports = {
Student, Course, Teacher
}
Step 3: Database connection can be easily established using mongoose like
mongoose.connect('mongodb://localhost:27017/GFG',
{
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
});
If database GFG is already present connection will be established otherwise the first database will be created and a connection will be established Here initially we have an empty database GFG as shown in the image below:

Initially database is empty
Create data objects, you want to insert, for all the collection then insert as shown in main.js file. As soon as we will insert data our collections will automatically be created.
Step 4: Write down the following code in the main.js file
Node
// main.js
const mongoose = require('mongoose');
const { Student, Course, Teacher } = require('./model.js');
// Connecting to database
mongoose.connect('mongodb://localhost:27017/GFG',
{
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
});
// Creating array of course data object
const courseData = [{
_id: 01,
name: "NodeJS",
category: "Backend"
},
{
_id: 02,
name: "MongoDB",
category: "Database"
}]
// Creating array of student data objects
const studentData = [{
name: "John",
enroll: 1801,
courseId: 01
}]
// Creating array of teacher data object
const teacherData = [{
name: "TeacherX",
teacher_id: 9901,
courseId: 01
},
{
name: "TeacherY",
teacher_id: 9902,
courseId: 02
}]
// Inserting course data
Course.insertMany(courseData)
.then(value => {
console.log("Saved Successfully");
})
.catch(error => {
console.log(error);
})
// Inserting student data
Student.insertMany(studentData)
.then(value => {
console.log("Saved Successfully");
})
.catch(error => {
console.log(error);
})
// Inserting teacher data
Teacher.insertMany(teacherData)
.then(value => {
console.log("Saved Successfully");
})
.catch(error => {
console.log(error);
})
Step to Run Application: Run the application using the following command from the root directory of the project
node main.js

Output after executing main.js
Database: Now we can see that three collections courses, students, teachers are created in our database GFG.

Database after the creation of multiple collections
Similar Reads
How to drop collection in MongoDb using Node.js ?
MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This fo
2 min read
How to create new Collection in MongoDB using Node.js ?
MongoDB the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This for
1 min read
How to rename the collection name of MongoDb using Node.js ?
MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This fo
2 min read
How to Join Two Collections in Mongodb using Node.js ?
Joining two collections in MongoDB using Node.js can be accomplished using the aggregation framework. The $lookup stage in the aggregation pipeline allows you to perform a left outer join to another collection in the same database. Understanding MongoDB CollectionsIn MongoDB, a collection is a group
4 min read
How to delete single and multiple documents in MongoDB using node.js ?
MongoDB, the most popular NoSQL database is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This for
2 min read
How to add Timestamp in Mongodb Collection using Node.js ?
Timestamp: With the help of timestamp document in the collection of the MongoDB can be differentiated on the basis of time. We can add Timestamp in Mongodb Collection in Node.js using the following approach: Installing Module: Install the mongoose module using the following command: npm install mong
1 min read
How to add range in the Collection of Mongodb using Node.js ?
Mongoose.module is one of the most powerful external module of the node.js.Mongoose is a MongoDB ODM i.e (Object database Modelling) that used to translate the code and its representation from MongoDB to the Node.js server.Mongoose module provides several functions in order to manipulate the documen
2 min read
How to fetch single and multiple documents from MongoDb using Node.js ?
MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This fo
2 min read
How to insert single and multiple documents in Mongodb using Node.js ?
MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This fo
2 min read
How to update single and multiple documents in MongoDB using Node.js ?
MongoDB the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This for
2 min read