Skip to content

Commit 2f9fb26

Browse files
authored
Merge pull request #13929 from Automattic/vkarpov15/gh-13760
fix(query): allow deselecting discriminator key using `-` syntax
2 parents 34fea9e + 391269f commit 2f9fb26

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lib/queryhelpers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ exports.applyPaths = function applyPaths(fields, schema) {
199199
// projection if `name` has schema-level `select: true`.
200200
if ((!type || !type.selected) || exclude !== false) {
201201
fields[path] = 0;
202+
exclude = true;
202203
} else if (type && type.selected && exclude === false) {
203204
// Make a note of minus paths that are overwriting paths that are
204205
// included by default.

test/query.test.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4171,14 +4171,22 @@ describe('Query', function() {
41714171
assert.equal(q.op, 'findOneAndReplace');
41724172
});
41734173

4174-
it('allows deselecting discriminator key (gh-13679)', async function() {
4175-
const testSchema = new Schema({ name: String });
4174+
it('allows deselecting discriminator key (gh-13760) (gh-13679)', async function() {
4175+
const testSchema = new Schema({ name: String, age: Number });
41764176
const Test = db.model('Test', testSchema);
41774177
const Test2 = Test.discriminator('Test2', new Schema({ test: String }));
41784178

41794179
const { _id } = await Test2.create({ name: 'test1', test: 'test2' });
41804180
const { name, __t } = await Test.findById(_id).select({ __t: 0 });
41814181
assert.strictEqual(name, 'test1');
41824182
assert.strictEqual(__t, undefined);
4183+
4184+
let doc = await Test.findById(_id).select({ __t: 0, age: 0 });
4185+
assert.strictEqual(doc.name, 'test1');
4186+
assert.strictEqual(doc.__t, undefined);
4187+
4188+
doc = await Test.findById(_id).select(['-__t', '-age']);
4189+
assert.strictEqual(doc.name, 'test1');
4190+
assert.strictEqual(doc.__t, undefined);
41834191
});
41844192
});

0 commit comments

Comments
 (0)