Installation of Mongoose module

Step 1: You can install this package by using this command.

npm install mongoose

Step 2: After installing the mongoose module, you can check your mongoose version in the command prompt using the command.

npm version mongoose

Step 3: 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

Following is the dataset, We are using in the following examples.

Project Structure:

Example: Finding and Updating Document

In this example, I am updating the company to Google the document with _id 2.

JavaScript
//Person.js

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

let PersonSchema = new Schema({
    _id: Number,
    first_name: String,
    last_name: String,
    email: String,
    gender: String,
    city: String,
    company_name: String,
})

module.exports = mongoose.model('Person', PersonSchema);
JavaScript
//index.js

const Person = require("../mongoose/model/person");
const mongoose = require("mongoose");

let mongoDB = mongoose.connect(
    "mongodb://localhost:27017/person",
    {
        useNewUrlParser: true,
        useUnifiedTopology: true,
    });

let db = mongoose.connection;
db.on("error", console.error.bind(console,
    "MongoDB Connection Error"));

(async () => {
    const res = await Person.findByIdAndUpdate(
        2,
        { company_name: "Google" }
    );
    console.log(res);
})();


Step to Run Application: Run the application using the following command from the root directory of the project:

node index.js

Console Output

After the Query:

Document after the query was run

Example: Updating Document

In this example, we are updating the gender to female in the third document.

JavaScript
//index.js

const Person = require("../mongoose/model/person");
const mongoose = require("mongoose");

let mongoDB = mongoose.connect(
    "mongodb://localhost:27017/person", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});

let db = mongoose.connection;
db.on("error", console.error.bind(console, 
    "MongoDB Connection Error"));

(async () => {
    const res = await Person.updateOne(
        { _id: 3 },
        { gender: "Female" }
    );
})();

Step to Run Application: Run the application using the following command from the root directory of the project:

node index.js

Before the Query:

Document before the query was run

After the Query:

Document after the query was run

Example: Deleting Document

In this example, we are trying to delete a document with last_name=Bourgeois. As there is a document that matches the condition, it will be deleted.

JavaScript
//index.js

const Person = require("../mongoose/model/person");
const mongoose = require("mongoose");

let mongoDB = mongoose.connect
    ("mongodb://localhost:27017/person", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});

let db = mongoose.connection;
db.on("error", console.error.bind(console, "
    MongoDB Connection Error"));

(async () => {
    const res = await Person.deleteOne
        ({ last_name: "Bourgeois" });
    console.log(`Number of Deleted Documents: 
        ${res.deletedCount}`);
})();

Step to Run Application: Run the application using the following command from the root directory of the project:

node index.js

Console Output:

Number of deleted documents



npm mongoose

NPM, which stands for Node Package Manager, is the default package manager of Node.js. As the default package manager, NPM is used to manage all the packages and the modules in Node.js. We can do so using the command line client npm.

In this article, we are going to learn about the Mongoose package provided by npm.

Similar Reads

Prerequisites

Node.js and npm JavaScript...

What is Mongoose?

The Mongoose module is one of Node.js’s most potent external modules. To transfer the code and its representation from MongoDB to the Node.js server, Mongoose is a MongoDB ODM (Object Database Modelling) tool....

Installation of Mongoose module:

Step 1: You can install this package by using this command....