Mongoose Module Introduction
Last Updated :
06 Oct, 2025
Mongoose is an ODM(Object Data Modelling) library for MongoDB and Node.js that provides a structured way to define models, perform CRUD operations, manage data relationships, and ensure consistency over MongoDB’s flexible NoSQL database.
- Node.js uses Mongoose for data interaction.
- Mongoose (ODM) provides Object Mapping and schemas, turning JavaScript objects into MongoDB documents.
- Mongoose translates high-level operations into commands for the Mongo Driver.
- The Mongo Driver communicates directly with the MongoDB database.
Why Use Mongoose with MongoDB?
MongoDB is a flexible, schema-less NoSQL database. Mongoose adds a layer to enforce schemas and validations while keeping this flexibility, which can also lead to challenges.
- Lack of Structure: MongoDB collections are schema-less, meaning that records in the same collection can have different fields or structures.
- Data Integrity: The absence of a defined schema makes it harder to ensure consistency across your data.
- Validation Issues: Since MongoDB doesn’t enforce any constraints or validation by default, it's easy to introduce invalid or incorrect data.
We can add any new key in any record according to the need. There is no proper structure for the MongoDB collections and constraints on the collections. Let's have a look at an example.
MongoDB:
Database: GFG
Collection: GFG1

In the above example, we can easily see there is no proper schema for a collection in MongoDB. We can use any numbers of different keys and value in the collection. This phenomenon might create some troubles. So let's see how can we overcome this problem.
Setting Up a Mongoose Application
Before we dive into using Mongoose, let's set up a simple Node.js application with Mongoose to understand how it works.
Step 1: Install Mongoose
First, create a new Node.js application if you haven't already:
npm init -y
Then, install Mongoose:
npm install mongoose
Step 2: Create the Project Structure
Here’s an example of how your project structure could look:

Step 3: Setting Up the MongoDB Server
Make sure you have a running MongoDB server. You can run MongoDB locally using the following command:
mongod --dbpath=data --bind_ip 127.0.0.1

Step 4: Implementing a Schema in Mongoose
In the index.js
file, let’s define a MongoDB schema using Mongoose.
// Importing mongoose module
const mongoose = require("mongoose")
// Database Address
const url = "mongodb://localhost:27017/GFG"
// Connecting to database
mongoose.connect(url).then((ans) => {
console.log("ConnectedSuccessful")
}).catch((err) => {
console.log("Error in the Connection")
})
// Calling Schema class
const Schema = mongoose.Schema;
// Creating Structure of the collection
const collection_structure = new Schema({
name: {
type: String,
require: true
},
marks: {
type: Number,
default: 0
}
})
// Creating collection
const collections = mongoose.model(
"GFG2", collection_structure)
// Inserting one document
collections.create({
name: "aayush"
}).then((ans) => {
console.log("Document inserted")
// Inserting invalid document
collections.create({
name: "saini",
marks: "#234",
phone: 981
}).then((ans) => {
console.log(ans)
}).catch((err) => {
// Printing the documents
collections.find().then((ans) => {
console.log(ans)
})
// Printing the Error Message
console.log(err.message)
})
}).catch((err) => {
// Printing Error Message
console.log(err.message)
})
Explanation:
- Schema Definition: We created a schema for the collection with a name field (which is required) and marks (with a default value of 0).
- Validation: We inserted a valid document, followed by an invalid document (with the wrong data type for
marks
), which will trigger a validation error. - Error Handling: The code uses
.catch()
to handle errors, such as data type mismatches, and prints appropriate messages.
Step 5: Running the Application
To run your Node.js application, execute the following command in the project directory:
node index.js
Console output:

Mongoose module imposed a definite structure on the collection and makes the collection rigid.
Explore
MongoDB Tutorial
7 min read
Introduction
Installation
Basics of MongoDB
MongoDB Methods
Comparison Operators
Logical Operators
Arithmetic Operators
Field Update Operators
Array Expression Operators