Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
Mongoose Query.prototype.mod() API
Next article icon

Mongoose Query API.prototype.writeConcern() Method

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

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
// Require mongoose module const mongoose = require("mongoose");  // Set Up the Database connection const URI = "mongodb://localhost:27017/geeksforgeeks"  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
// Require mongoose module const mongoose = require("mongoose");  // Set Up the Database connection const URI = "mongodb://localhost:27017/geeksforgeeks"  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


Next Article
Mongoose Query.prototype.mod() API

S

sakshio0hoj
Improve
Article Tags :
  • Web Technologies
  • Node.js
  • Mongoose
  • Mongoose-API

Similar Reads

  • Mongoose Query.prototype.readConcern() API
    The Mongoose Query API.prototype.readConcern() method of the Mongoose API is used on the Query objects. It allows us to set and configure the read concern query options for a particular query object. Using this method we can control the consistency and isolation properties of the data from different
    3 min read
  • Mongoose Query.prototype.replaceOne() API
    The Mongoose Query API replaceOne() method is used to replace a single document from a collection using the MongoDB query system. It can update only one document at a time. Syntax: Query.prototype.replaceOne(filter, doc, options, callback) Parameters: It accepts the following 4 parameters as mention
    3 min read
  • Mongoose Query.prototype.remove() API
    The Mongoose Query API remove() method is used to remove single or multiple documents, from a collection using the MongoDB query system.  Syntax: Query.prototype.remove(filter, callback) Parameters: It accepts the following 2 parameters as mentioned above and described below: filter: It is a mongoos
    3 min read
  • Mongoose Query.prototype.mod() API
    The Mongoose Query API.prototype.mod() method of the Mongoose API is used on the Query objects. It allows us to provide condition in the form of modulus. Using this method we can select the documents where the value of the field divided by a divisor has the specified remainder. Let us understand mod
    3 min read
  • Mongoose Query.prototype.updateOne() API
    The Mongoose Query API.prototype.updateOne() method of the Mongoose API is used on the Query objects. It allows us to update the document in the collection. MongoDB updates the first document that matches the filter regardless of the multi-option value. Let us understand updateOne() method using an
    3 min read
  • Mongoose Query.prototype.then() API
    Mongoose is an Object Data Modeling (ODM) library for MongoDB. It defines a strongly-typed schema, with default values and schema validations which are later mapped to a MongoDB document.  The Mongoose Query API.prototype.then() method returns a Promise after executing the query. If any occurs while
    3 min read
  • Mongoose Query.prototype.toConstructor() API
    The Mongoose Query API.prototype.toConstructor() method of the Mongoose API is used on the Query objects. It allows us to customize the query constructor. Using this method we can make reusable constructors with all the configured properties and options. Let us understand toConstructor() method usin
    3 min read
  • Mongoose Query.prototype.model API
    The Mongoose Query API.prototype.model property of the Mongoose API is used on the Query objects. It allows us to verify the model with which the current query is associated with. Let us understand model property using an example. Syntax: query.model; Parameters: This property does not accept any pa
    2 min read
  • Mongoose Query.prototype.get() API
    The Mongoose Query API.prototype.get() method of the Mongoose API is used on the Query objects. It allows us to get the latest value for the fields we are updating using mongoose update method. Usually this method is used for update operations to get the updated value for a particular path. Let us u
    3 min read
  • Mongoose Query.prototype.$where() API
    The Mongoose Query API.prototype.$where() method of the Mongoose API is used on the Query objects. It allows us to put the where condition in the form of JavaScript object or a function in order to pass the expression to the mongodb system. Let us understand the $where() method using an example. Syn
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences