Skip to content

Commit f24953c

Browse files
committedMar 30, 2021
fix(query): add writeConcern() method to avoid writeConcern deprecation warning
Fix #10009
1 parent 7d2e9c9 commit f24953c

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
 

‎lib/query.js

+47
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,53 @@ Query.prototype.session = function session(v) {
10831083
return this;
10841084
};
10851085

1086+
/**
1087+
* Sets the 3 write concern parameters for this query:
1088+
*
1089+
* - `w`: Sets the specified number of `mongod` servers, or tag set of `mongod` servers, that must acknowledge this write before this write is considered successful.
1090+
* - `j`: Boolean, set to `true` to request acknowledgement that this operation has been persisted to MongoDB's on-disk journal.
1091+
* - `wtimeout`: If [`w > 1`](/docs/api.html#query_Query-w), the maximum amount of time to wait for this write to propagate through the replica set before this operation fails. The default is `0`, which means no timeout.
1092+
*
1093+
* This option is only valid for operations that write to the database:
1094+
*
1095+
* - `deleteOne()`
1096+
* - `deleteMany()`
1097+
* - `findOneAndDelete()`
1098+
* - `findOneAndReplace()`
1099+
* - `findOneAndUpdate()`
1100+
* - `remove()`
1101+
* - `update()`
1102+
* - `updateOne()`
1103+
* - `updateMany()`
1104+
*
1105+
* Defaults to the schema's [`writeConcern` option](/docs/guide.html#writeConcern)
1106+
*
1107+
* ####Example:
1108+
*
1109+
* // The 'majority' option means the `deleteOne()` promise won't resolve
1110+
* // until the `deleteOne()` has propagated to the majority of the replica set
1111+
* await mongoose.model('Person').
1112+
* deleteOne({ name: 'Ned Stark' }).
1113+
* writeConcern({ w: 'majority' });
1114+
*
1115+
* @method writeConcern
1116+
* @memberOf Query
1117+
* @instance
1118+
* @param {Object} writeConcern the write concern value to set
1119+
* @see mongodb https://mongodb.github.io/node-mongodb-native/3.1/api/global.html#WriteConcern
1120+
* @return {Query} this
1121+
* @api public
1122+
*/
1123+
1124+
Query.prototype.writeConcern = function writeConcern(val) {
1125+
if (val == null) {
1126+
delete this.options.writeConcern;
1127+
return this;
1128+
}
1129+
this.options.writeConcern = val;
1130+
return this;
1131+
};
1132+
10861133
/**
10871134
* Sets the specified number of `mongod` servers, or tag set of `mongod` servers,
10881135
* that must acknowledge this write before this write is considered successful.

‎test/query.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -3776,4 +3776,16 @@ describe('Query', function() {
37763776

37773777
assert.deepEqual(q._fields, { doesntpopulate: 0, populatescorrectly: 0 });
37783778
});
3779+
3780+
it('sets `writeConcern` option correctly (gh-10009)', function() {
3781+
const testSchema = new mongoose.Schema({
3782+
name: String
3783+
});
3784+
const Test = db.model('Test', testSchema);
3785+
3786+
const q = Test.find();
3787+
q.writeConcern({ w: 'majority', wtimeout: 1000 });
3788+
3789+
assert.deepEqual(q.options.writeConcern, { w: 'majority', wtimeout: 1000 });
3790+
});
37793791
});

0 commit comments

Comments
 (0)
Please sign in to comment.