Horje
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.

Prerequisites

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.

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.

Peek202210251450

Project Structure:

Screenshotfrom202210310849181

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

Screenshotfrom20221025222413

After the Query:

Screenshot-2024-05-05-at-15041-PM

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:

Screenshot-2024-05-05-at-15545-PM

Document before the query was run

After the Query:

Screenshot-2024-05-05-at-15553-PM

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:

Screenshotfrom20221026171702

Number of deleted documents




Reffered: https://www.geeksforgeeks.org


Web Technologies

Related
How to Optimize Images for WordPress? How to Optimize Images for WordPress?
Top 10 Angular features you should know Top 10 Angular features you should know
How to Speed Up WordPress Page Loading Time? How to Speed Up WordPress Page Loading Time?
Web3.0: The Next Era of the Internet Web3.0: The Next Era of the Internet
XML - CDATA Sections XML - CDATA Sections

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
15