Open In App

Mongoose Query.prototype.setUpdate() API

Last Updated : 13 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Mongoose Query API.prototype.setUpdate() method of the Mongoose API is used on the Query objects. It allows us to set the current update operation with the new values and expressions. Using this method we can modify update query expression. Let us understand setUpdate() method using an example.

Syntax:

query.setUpdate( object );

Parameters: This method accepts a single parameter as discussed below:

  • object: It is used to specify the update query expression in the form of object.

Return Value: This method returns undefined or returns nothing.

Setting up Node.js application:

Step 1: Create a Node.js application using the following command:

npm init

Step 2: After creating the NodeJS application, Install the required module using the following command:

npm install mongoose

Project Structure: The project structure will look like this:

 

Database Structure: The database structure will look like this, the following documents are present in the collection.

 

Example 1: In this example, we are illustrating the functionality of setUpdate() method. We are updating orderNumber field for one of the document in collection. Using setUpdate() method we are changing the query expression and update value.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const Customer = connectionObject.model(
    "Customer",
    new mongoose.Schema({
        name: String,
        address: String,
        orderNumber: Number,
    })
);
  
const query = Customer.find({ name: "Bhavesh" });
query.update({}, { $set: { orderNumber: 6 } })
query.setUpdate({ $set: { orderNumber: 0 } })
console.log(query.getUpdate());
query.exec((error, result) => {
    if (error) {
        console.log("Error -", error);
    } else {
        console.log("Result -", result);
    }
})


Step to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

{ '$set': { orderNumber: 0 } }
Result - {
  acknowledged: true,
  modifiedCount: 1,
  upsertedId: null,
  upsertedCount: 0,

GUI Representation of the Database using Robo3T GUI tool:

 

Example 2: In this example, we are illustrating the functionality of setUpdate() method. We are updating orderNumber field for one of the document in collection. Using setUpdate() method have changed the field we were updating using update() method. At the end, we are updating only one field i.e orderNumber.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const Customer = connectionObject.model(
    "Customer",
    new mongoose.Schema({
        name: String,
        address: String,
        orderNumber: Number,
    })
);
  
const query = Customer.find({ name: "Aditya" });
query.update({}, { $set: { name: "Aaditya" } })
query.setUpdate({ $set: { orderNumber: 20 } })
console.log(query.getUpdate());
query.then((result) => {
    console.log("Result -", result);
}).catch((err) => {
    console.log(err);
})


Step to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

{ '$set': { orderNumber: 20 } }

Result - {
  acknowledged: true,
  modifiedCount: 1,  
  upsertedId: null,  
  upsertedCount: 0,  
  matchedCount: 1    
}

GUI Representation of the Database using Robo3T GUI tool:

 

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads