Skip to content

Commit d24b7a8

Browse files
authored
Merge pull request #14562 from Automattic/vkarpov15/gh-14521
fix(query): apply translateAliases before casting to avoid strictMode error when using aliases
2 parents ddb19fa + 090f23a commit d24b7a8

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

lib/query.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2222,6 +2222,7 @@ function _castArrayFilters(query) {
22222222
* @api private
22232223
*/
22242224
Query.prototype._find = async function _find() {
2225+
this._applyTranslateAliases();
22252226
this._castConditions();
22262227

22272228
if (this.error() != null) {
@@ -2244,8 +2245,6 @@ Query.prototype._find = async function _find() {
22442245

22452246
const options = this._optionsForExec();
22462247

2247-
this._applyTranslateAliases(options);
2248-
22492248
const filter = this._conditions;
22502249
const fields = options.projection;
22512250

@@ -2510,6 +2509,7 @@ Query.prototype._completeMany = async function _completeMany(docs, fields, userP
25102509
*/
25112510

25122511
Query.prototype._findOne = async function _findOne() {
2512+
this._applyTranslateAliases();
25132513
this._castConditions();
25142514

25152515
if (this.error()) {
@@ -2522,8 +2522,6 @@ Query.prototype._findOne = async function _findOne() {
25222522

25232523
const options = this._optionsForExec();
25242524

2525-
this._applyTranslateAliases(options);
2526-
25272525
// don't pass in the conditions because we already merged them in
25282526
const doc = await this.mongooseCollection.findOne(this._conditions, options);
25292527
return new Promise((resolve, reject) => {
@@ -2604,6 +2602,8 @@ Query.prototype.findOne = function(conditions, projection, options) {
26042602
*/
26052603

26062604
Query.prototype._countDocuments = async function _countDocuments() {
2605+
this._applyTranslateAliases();
2606+
26072607
try {
26082608
this.cast(this.model);
26092609
} catch (err) {
@@ -2619,8 +2619,6 @@ Query.prototype._countDocuments = async function _countDocuments() {
26192619

26202620
const options = this._optionsForExec();
26212621

2622-
this._applyTranslateAliases(options);
2623-
26242622
const conds = this._conditions;
26252623

26262624
return this.mongooseCollection.countDocuments(conds, options);
@@ -2631,7 +2629,7 @@ Query.prototype._countDocuments = async function _countDocuments() {
26312629
* on the following query properties: filter, projection, update, distinct.
26322630
*/
26332631

2634-
Query.prototype._applyTranslateAliases = function _applyTranslateAliases(options) {
2632+
Query.prototype._applyTranslateAliases = function _applyTranslateAliases() {
26352633
let applyTranslateAliases = false;
26362634
if ('translateAliases' in this._mongooseOptions) {
26372635
applyTranslateAliases = this._mongooseOptions.translateAliases;
@@ -2646,7 +2644,7 @@ Query.prototype._applyTranslateAliases = function _applyTranslateAliases(options
26462644

26472645
if (this.model?.schema?.aliases && Object.keys(this.model.schema.aliases).length > 0) {
26482646
this.model.translateAliases(this._conditions, true);
2649-
this.model.translateAliases(options.projection, true);
2647+
this.model.translateAliases(this._fields, true);
26502648
this.model.translateAliases(this._update, true);
26512649
if (this._distinct != null && this.model.schema.aliases[this._distinct] != null) {
26522650
this._distinct = this.model.schema.aliases[this._distinct];
@@ -2777,6 +2775,7 @@ Query.prototype.countDocuments = function(conditions, options) {
27772775
*/
27782776

27792777
Query.prototype.__distinct = async function __distinct() {
2778+
this._applyTranslateAliases();
27802779
this._castConditions();
27812780

27822781
if (this.error()) {
@@ -2787,7 +2786,6 @@ Query.prototype.__distinct = async function __distinct() {
27872786
applyGlobalDiskUse(this.options, this.model.db.options, this.model.base.options);
27882787

27892788
const options = this._optionsForExec();
2790-
this._applyTranslateAliases(options);
27912789

27922790
return this.mongooseCollection.
27932791
distinct(this._distinct, this._conditions, options);
@@ -3006,14 +3004,14 @@ Query.prototype.deleteOne = function deleteOne(filter, options) {
30063004
*/
30073005

30083006
Query.prototype._deleteOne = async function _deleteOne() {
3007+
this._applyTranslateAliases();
30093008
this._castConditions();
30103009

30113010
if (this.error() != null) {
30123011
throw this.error();
30133012
}
30143013

30153014
const options = this._optionsForExec();
3016-
this._applyTranslateAliases(options);
30173015

30183016
return this.mongooseCollection.deleteOne(this._conditions, options);
30193017
};
@@ -3080,14 +3078,14 @@ Query.prototype.deleteMany = function(filter, options) {
30803078
*/
30813079

30823080
Query.prototype._deleteMany = async function _deleteMany() {
3081+
this._applyTranslateAliases();
30833082
this._castConditions();
30843083

30853084
if (this.error() != null) {
30863085
throw this.error();
30873086
}
30883087

30893088
const options = this._optionsForExec();
3090-
this._applyTranslateAliases(options);
30913089

30923090
return this.mongooseCollection.deleteMany(this._conditions, options);
30933091
};
@@ -3277,6 +3275,7 @@ Query.prototype.findOneAndUpdate = function(filter, doc, options) {
32773275
*/
32783276

32793277
Query.prototype._findOneAndUpdate = async function _findOneAndUpdate() {
3278+
this._applyTranslateAliases();
32803279
this._castConditions();
32813280

32823281
_castArrayFilters(this);
@@ -3293,7 +3292,6 @@ Query.prototype._findOneAndUpdate = async function _findOneAndUpdate() {
32933292
}
32943293
const options = this._optionsForExec(this.model);
32953294
convertNewToReturnDocument(options);
3296-
this._applyTranslateAliases(options);
32973295

32983296
this._update = this._castUpdate(this._update);
32993297

@@ -3418,6 +3416,7 @@ Query.prototype.findOneAndDelete = function(filter, options) {
34183416
* @api private
34193417
*/
34203418
Query.prototype._findOneAndDelete = async function _findOneAndDelete() {
3419+
this._applyTranslateAliases();
34213420
this._castConditions();
34223421

34233422
if (this.error() != null) {
@@ -3428,7 +3427,6 @@ Query.prototype._findOneAndDelete = async function _findOneAndDelete() {
34283427

34293428
const filter = this._conditions;
34303429
const options = this._optionsForExec(this.model);
3431-
this._applyTranslateAliases(options);
34323430

34333431
let res = await this.mongooseCollection.findOneAndDelete(filter, options);
34343432
for (const fn of this._transforms) {
@@ -3542,6 +3540,7 @@ Query.prototype.findOneAndReplace = function(filter, replacement, options) {
35423540
* @api private
35433541
*/
35443542
Query.prototype._findOneAndReplace = async function _findOneAndReplace() {
3543+
this._applyTranslateAliases();
35453544
this._castConditions();
35463545
if (this.error() != null) {
35473546
throw this.error();
@@ -3554,7 +3553,6 @@ Query.prototype._findOneAndReplace = async function _findOneAndReplace() {
35543553

35553554
const filter = this._conditions;
35563555
const options = this._optionsForExec();
3557-
this._applyTranslateAliases(options);
35583556
convertNewToReturnDocument(options);
35593557

35603558
const includeResultMetadata = this.options.includeResultMetadata;
@@ -3749,6 +3747,8 @@ Query.prototype._mergeUpdate = function(doc) {
37493747
*/
37503748

37513749
async function _updateThunk(op) {
3750+
this._applyTranslateAliases();
3751+
37523752
this._castConditions();
37533753

37543754
_castArrayFilters(this);
@@ -3759,7 +3759,6 @@ async function _updateThunk(op) {
37593759

37603760
const castedQuery = this._conditions;
37613761
const options = this._optionsForExec(this.model);
3762-
this._applyTranslateAliases(options);
37633762

37643763
this._update = clone(this._update, options);
37653764
const isOverwriting = op === 'replaceOne';

test/query.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3718,6 +3718,28 @@ describe('Query', function() {
37183718
}, /Provided object has both field "n" and its alias "name"/);
37193719
});
37203720

3721+
it('translateAliases applies before casting (gh-14521) (gh-7511)', async function() {
3722+
const testSchema = new Schema({
3723+
name: {
3724+
type: String,
3725+
alias: 'n'
3726+
},
3727+
age: {
3728+
type: Number
3729+
}
3730+
});
3731+
const Test = db.model('Test', testSchema);
3732+
3733+
const doc = await Test.findOneAndUpdate(
3734+
{ n: 14521 },
3735+
{ age: 7511 },
3736+
{ translateAliases: true, upsert: true, returnDocument: 'after' }
3737+
);
3738+
3739+
assert.strictEqual(doc.name, '14521');
3740+
assert.strictEqual(doc.age, 7511);
3741+
});
3742+
37213743
it('schema level translateAliases option (gh-7511)', async function() {
37223744
const testSchema = new Schema({
37233745
name: {

0 commit comments

Comments
 (0)