Open In App

Mongoose Query.prototype.wtimeout() API

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

The Mongoose Query API.prototype.wtimeout() method of the Mongoose API is used on the Query objects. It allows us to set the specific time the operation should take to complete the execution. Using this method we can set the maximum amount of time particular method should take to complete its write operation to specified number of replica sets, if takes longer than that the operation will fail. Let us understand wtimeout() method using an example.

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

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

Syntax:

query.deleteOne(...).w(...).wtimeout( <time/ms> );

 

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

  • ms: It is used to specify the maximum time method should take to execute the operation. Time will be in milliseconds.

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 wtimeout() method by providing maximum time upto 2 seconds to complete the update operation to the specified number of mongodb servers.

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.findOneAndUpdate(
    { _id: "639ede899fdf57759087a655" },
    { orderNumber: 0 }
);
  
query.w(2).wtimeout(2000);
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:

GUI Representation of the  Database using Robo3T GUI tool:

 

Example 2: In this example, we are illustrating the functionality of wtimeout() method by finding and deleting the document object from the collection.

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.findOneAndDelete(
    { _id: "63b658a5876922405349a40d" }
);
  
query.w("majority").wtimeout(500);
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:

GUI Representation of the  Database using Robo3T GUI tool:

 

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads