Skip to content

Commit c05baa5

Browse files
committed
fix(query): propagate read concern to populate if readConcern() called after populate()
Re: #15553
1 parent 3ca038e commit c05baa5

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

lib/query.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,13 +2133,25 @@ Query.prototype._optionsForExec = function(model) {
21332133
}
21342134
}
21352135

2136-
if (this._mongooseOptions.populate && options.readPreference) {
2137-
for (const pop of Object.values(this._mongooseOptions.populate)) {
2138-
if (pop.options?.readPreference === undefined) {
2139-
if (!pop.options) {
2140-
pop.options = {};
2136+
if (this._mongooseOptions.populate) {
2137+
if (options.readPreference) {
2138+
for (const pop of Object.values(this._mongooseOptions.populate)) {
2139+
if (pop.options?.readPreference === undefined) {
2140+
if (!pop.options) {
2141+
pop.options = {};
2142+
}
2143+
pop.options.readPreference = options.readPreference;
2144+
}
2145+
}
2146+
}
2147+
if (options.readConcern) {
2148+
for (const pop of Object.values(this._mongooseOptions.populate)) {
2149+
if (pop.options?.readConcern === undefined) {
2150+
if (!pop.options) {
2151+
pop.options = {};
2152+
}
2153+
pop.options.readConcern = options.readConcern;
21412154
}
2142-
pop.options.readPreference = options.readPreference;
21432155
}
21442156
}
21452157
}
@@ -4929,18 +4941,6 @@ Query.prototype.populate = function() {
49294941

49304942
const res = utils.populate.apply(null, args);
49314943

4932-
// Propagate readConcern from parent query, unless one already specified. Read pref propagated separately
4933-
if (this.options != null) {
4934-
const readConcern = this.options.readConcern;
4935-
4936-
for (const populateOptions of res) {
4937-
if (readConcern != null && (populateOptions && populateOptions.options && populateOptions.options.readConcern) == null) {
4938-
populateOptions.options = populateOptions.options || {};
4939-
populateOptions.options.readConcern = readConcern;
4940-
}
4941-
}
4942-
}
4943-
49444944
const opts = this._mongooseOptions;
49454945

49464946
if (opts.lean != null) {

test/query.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4492,6 +4492,29 @@ describe('Query', function() {
44924492
assert.strictEqual(query._mongooseOptions.populate.friends.options.readPreference, 'secondaryPreferred');
44934493
});
44944494

4495+
it('propagates readConcern to populate options if readConcern() is called after populate() (gh-15553)', async function() {
4496+
const schema = new Schema({ name: String, age: Number, friends: [{ type: 'ObjectId', ref: 'Person' }] });
4497+
const Person = db.model('Person', schema);
4498+
4499+
let query = Person.find({}).populate('friends');
4500+
query.readConcern('majority');
4501+
await query.exec();
4502+
assert.strictEqual(query._mongooseOptions.populate.friends.options.readConcern.level, 'majority');
4503+
4504+
query = Person.find({}).readConcern('local').populate('friends');
4505+
query.readConcern('majority');
4506+
await query.exec();
4507+
assert.strictEqual(query._mongooseOptions.populate.friends.options.readConcern.level, 'majority');
4508+
4509+
query = Person.find({}).readConcern('majority').populate('friends');
4510+
await query.exec();
4511+
assert.strictEqual(query._mongooseOptions.populate.friends.options.readConcern.level, 'majority');
4512+
4513+
query = Person.find({}).readConcern('majority').populate({ path: 'friends', options: { readConcern: 'local' } });
4514+
await query.exec();
4515+
assert.strictEqual(query._mongooseOptions.populate.friends.options.readConcern, 'local');
4516+
});
4517+
44954518
describe('Query with requireFilter', function() {
44964519
let Person;
44974520
let _id;

0 commit comments

Comments
 (0)