Skip to content

Commit

Permalink
Merge pull request #11785 from Automattic/gh-11698
Browse files Browse the repository at this point in the history
Gh-11698, Prevents .$* from being put into projections by avoiding drilling down into maps
  • Loading branch information
vkarpov15 authored May 9, 2022
2 parents ef83dce + 85ba043 commit 7515b81
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/queryhelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ exports.applyPaths = function applyPaths(fields, schema) {
const addedPaths = [];
schema.eachPath(function(path, type) {
if (prefix) path = prefix + '.' + path;

if (type.$isSchemaMap || path.endsWith('.$*')) {
return;
}
let addedPath = analyzePath(path, type);
// arrays
if (addedPath == null && !Array.isArray(type) && type.$isMongooseArray && !type.$isMongooseDocumentArray) {
Expand All @@ -248,7 +250,6 @@ exports.applyPaths = function applyPaths(fields, schema) {
}
}
});

stack.pop();
return addedPaths;
}
Expand Down
18 changes: 18 additions & 0 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8503,6 +8503,24 @@ describe('Model', function() {
assert.deepEqual(indexes[1].key, { name: 1 });
assert.strictEqual(indexes[1].collation.locale, 'en');
});
it('prevents .$* from being put in projections by avoiding drilling down into maps (gh-11698)', async function() {
const subSchema = new Schema({
selected: { type: Number },
not_selected: { type: Number, select: false }
});

const testSchema = new Schema({
subdocument_mapping: {
type: Map,
of: subSchema
}
});


const Test = db.model('Test', testSchema);

assert.ok(await Test.find());
});
});


Expand Down

0 comments on commit 7515b81

Please sign in to comment.