Open In App

Mongoose Query.prototype.set() API

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Mongoose Query API set() method is used to add a $set middleware that helps in updating one or more than one MongoDB document depending on the path specified and the value passed in the argument.

Syntax:

Query.prototype.set(path, val)

Parameters: It accepts the following parameters as mentioned above and described below:

  • path: It is a string that identifies the path in the mongoose schema
  • val: It is a value that gets set on the path specified

Return type: It returns a Query object as a response.

Creating node application And Installing Mongoose:

Step 1: Create a node application using the following command:

mkdir folder_name
cd folder_name
npm init -y
touch main.js

Step 2: After completing the Node.js application, Install the required module using the following command

npm install mongoose

Example 1: In this example, we will use this method to update the name of one record whose name is "Luffy".

Filename: main.js

JavaScript
// Importing the module
const mongoose = require('mongoose')

// Creating the connection
mongoose.connect('mongodb://localhost:27017/query-helpers',
    {
        dbName: 'event_db',
        useNewUrlParser: true,
        useUnifiedTopology: true
    }, err => err ? console.log(err)
        : console.log('Connected to database'));

let personSchema = new mongoose.Schema({
    name: {
        type: String,
    },
    age: {
        type: Number,
    }
});

let personsArray = [
    {
        name: 'Luffy',
        age: 20
    },
    {
        name: 'Nami',
        age: 20,
    },
    {
        name: 'Zoro',
        age: 35
    }
]

let Person = mongoose.model('Person', personSchema);

(async () => {
    await Person.insertMany(personsArray)
    let res = await Person.updateOne({ name: 'Luffy' }).
        set({ name: 'Usorp' })
    console.log({ res });
})();


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

node main.js

Output:

 

GUI Representation of the  Database using MongoDB Compass:

 

Example 2: In this example, we will use this method to update the age of one record whose name is "Luffy"

Filename: main.js

JavaScript
// Importing the module
const mongoose = require('mongoose')

// Creating the connection
mongoose.connect('mongodb://localhost:27017/query-helpers',
    {
        dbName: 'event_db',
        useNewUrlParser: true,
        useUnifiedTopology: true
    }, err => err ? console.log(err)
        : console.log('Connected to database'));

let personSchema = new mongoose.Schema({
    name: {
        type: String,
    },
    age: {
        type: Number,
    }
});

let personsArray = [
    {
        name: 'Luffy',
        age: 20
    },
    {
        name: 'Nami',
        age: 20,
    },
    {
        name: 'Zoro',
        age: 35
    }
]

let Person = mongoose.model('Person', personSchema);

(async () => {
    await Person.insertMany(personsArray)
    let res = await Person.updateOne({ name: 'Luffy' }).
        set({ age: 19 })
    console.log({ res });
})();


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

node main.js

Output

 

GUI Representation of the  Database using MongoDB Compass:

 

Reference: https://mongoosejs.com/docs/api/query.html#query_Query-set


Next Article

Similar Reads