Open In App

Mongoose Query.prototype.w() API

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

The Mongoose Query API.prototype.w() method of the Mongoose API is used on the Query objects. It allows us to set the specified number of MongoDB servers that must acknowledge the write operations. before the write event is successfully executed. Providing “1” as a parameter to the method indicates acknowledgment by one server, and providing “majority” as a parameter to the method indicates acknowledgment from multiple replica sets of the server. Let us understand w() method using an example.

This option or w() method we can access on following methods:

  • deleteOne()
  • deleteMany()
  • findOneAndDelete()
  • findOneAndReplace()
  • findOneAndUpdate()
  • remove()
  • update()
  • updateOne()
  • updateMany()

Syntax:

query.deleteOne( ... ).w( val );

 

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

  • val: It is used to specify the number or string, 1 to get acknowledgment from 1 MongoDB server, “majority” to get acknowledgment from most of the replica sets.

Return Value: This method returns the query object.

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 w() method by accessing it on the updateOne() method. 

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,
    }));
  
(async () => {
    const query = Customer.updateOne(
        { name: "Chintu" }, { orderNumber: 10 }
    );
    query.w(1);
    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:

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

GUI Representation of the  Database using Robo3T GUI tool:

 

Example 2: In this example, we are illustrating the functionality of w() method by accessing it on the deleteOne() method.  At the end, we are deleting one document from collection, seeking for the acknowledgement from most of the replica sets.

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.deleteOne({ name: "Bhavesh" });
query.w("majority");
query.then(result => {
    console.log(result);
}).catch(error => {
    console.log(error);
})


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

node app.js

Output:

{ acknowledged: true, deletedCount: 1 }

GUI Representation of the  Database using Robo3T GUI tool:

 

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads