Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(query): select Map field with select: false when explicitly requested #12616

Merged
merged 1 commit into from Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/queryhelpers.js
Expand Up @@ -232,7 +232,9 @@ exports.applyPaths = function applyPaths(fields, schema) {
schema.eachPath(function(path, type) {
if (prefix) path = prefix + '.' + path;
if (type.$isSchemaMap || path.endsWith('.$*')) {
if (type.options && type.options.select === false) {
const plusPath = '+' + path;
const hasPlusPath = fields && plusPath in fields;
if (type.options && type.options.select === false && !hasPlusPath) {
excluded.push(path);
}
return;
Expand Down
28 changes: 28 additions & 0 deletions test/query.test.js
Expand Up @@ -4133,6 +4133,34 @@ describe('Query', function() {
assert.equal(item.doNotSelect, undefined);
});

it('Map field with select: false is selected when explicitly requested (gh-12603)', async function() {
const testSchema = new mongoose.Schema({
title: String,
body: {
type: Map,
of: { en: String, pt: String },
select: false
}
});

const Test = db.model('Test', testSchema);
await Test.create({
title: 'test',
body: {
A: { en: 'en test A value', pt: 'pt test A value' },
B: { en: 'en test B value', pt: 'pt test B value' }
}
});

const item = await Test.findOne({}).select('+body');
assert.equal(item.title, 'test');
assert.equal(item.get('body.A.en'), 'en test A value');

const item2 = await Test.findOne({}).select('body');
assert.equal(item2.title, undefined);
assert.equal(item2.get('body.A.en'), 'en test A value');
});

it('treats ObjectId as object with `_id` for `merge()` (gh-12325)', async function() {
const testSchema = new mongoose.Schema({ name: String });
const Test = db.model('Test', testSchema);
Expand Down