Open In App

What are the advantages of using Mongoose module?

Last Updated : 06 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Mongoose module is one of the most powerful external modules of NodeJS. 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.

Advantages of Mongoose module:

  1. Collection validation of the MongoDB database can be done easily.
  2. Predefined Structure can be implemented on the collection.
  3. Constraints can be applied to documents of collections using Mongoose.
  4. Mongoose module built on the top of MongoDB driver and provides easily abstraction of the query and define a query.

Many developers that are habitual of using SQL feel uncomfortable while working on the MongoDB because of Nosql databases and flexible structure. Here Mongoose plays an important role and makes the collection schema similar to the SQL databases.

Mongoose module provides several functions in order to manipulate the documents of the collection of the MongoDB database.

Implementation for definite structure and  Collection Validation: The mongoose module imposed a definite structure on the collection and makes the collection rigid.

Installing Module:

npm install mongoose

Project Structure:

Running the server on Local IP: Data is the directory where MongoDB server is present.

mongod --dbpath=data --bind_ip 127.0.0.1

Filename- index.js:

Javascript




// Importing mongoose module
const mongoose = require("mongoose")
 
// Database Address
 
// 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)
})


Run index.js file using below command:

node index.js

Output:

Implementation of the constraint on the collection of MongoDB:

Filename- index.js:

Javascript




// Importing mongoose module
const mongoose = require("mongoose")
 
// Database Address
 
// 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,
    unique: true,
    require: true
  }
  ,
  marks: {
    type: Number,
    default: 0
  }
})
 
// Creating collection
const collections = mongoose.model("GFG2", collection_structure)
 
// Inserting one document
collections.create({
  name: "aayush",
  marks: 12,
}).then((ans) => {
  console.log("Document inserted")
  // Inserting invalid document
}).catch((err) => {
  console.log(err.message);
})


Run index.js file using below command:

node index.js

Output: [First record is already present in the collection]



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads