Mongoose Query API.prototype.writeConcern() Method
The Mongoose Query API.prototype.writeConcern() method of the Mongoose API is used on Query objects. It allows us to set the options for writing events to the database. Using this method we can set various parameters to configure the write operations. Let us understand writeConcern() method using an example.
The writeConcern operations are available on the following methods:
- deleteOne()
- deleteMany()
- findOneAndDelete()
- findOneAndReplace()
- findOneAndUpdate()
- remove()
- update()
- updateOne()
- updateMany()
Syntax:
Model.deleteOne(...).writeConcern( <object> );
Parameters: This method accepts a single parameter as described below:
- object: It is used to specify various properties and their values in the form of an object.
Return Value: This method returns the query object.
Setting up Node.js Mongoose Module:
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: The below example illustrates the basic functionality of the Mongoose Connection writeConcern() method, using the asynchronous function.
Filename: app.js
Javascript
const mongoose = require( "mongoose" );
const connectionObject = mongoose.createConnection(URI, {
useNewUrlParser: true ,
useUnifiedTopology: true ,
});
const studentSchema = new mongoose.Schema({
name: { type: String },
age: { type: Number },
rollNumber: { type: Number },
});
const Student = connectionObject.model( 'Student' , studentSchema);
(async () => {
const query = await Student.updateOne({ name: 'Student4' }, { age: 42 }).
writeConcern({ w: 'majority' });
console.log(query);
})
();
|
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,
modifiedCount: 1,
upsertedId: null,
upsertedCount: 0,
matchedCount: 1
}
GUI Representation of the Database using Robo3T GUI tool:
Example 2: The below example illustrates the basic functionality of the Mongoose Connection writeConcern() method, using then block.
Filename: app.js
Javascript
const mongoose = require( "mongoose" );
const connectionObject = mongoose.createConnection(URI, {
useNewUrlParser: true ,
useUnifiedTopology: true ,
});
const studentSchema = new mongoose.Schema({
name: { type: String },
age: { type: Number },
rollNumber: { type: Number },
});
const Student = connectionObject.model( 'Student' , studentSchema);
Student.deleteOne({ name: 'Student4' }).writeConcern({ w: 'majority' , j: true }).
then(queryObject => {
console.log(queryObject);
});
|
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 }
Reference: https://mongoosejs.com/docs/api/query.html#query_Query-writeConcern
Last Updated :
10 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...