Skip to content

Commit

Permalink
Merge pull request #13722 from Automattic/vkarpov15/gh-13679
Browse files Browse the repository at this point in the history
fix(query): allow deselecting discriminator key
  • Loading branch information
vkarpov15 committed Aug 11, 2023
2 parents a88b73f + d783c03 commit 48edaf1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/queryhelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ exports.applyPaths = function applyPaths(fields, schema) {
// If set to 0, we're explicitly excluding the discriminator key. Can't do this for all fields,
// because we have tests that assert that using `-path` to exclude schema-level `select: true`
// fields counts as an exclusive projection. See gh-11546
if (exclude && type.selected && path === schema.options.discriminatorKey && fields[path] != null && !fields[path]) {
if (!exclude && type.selected && path === schema.options.discriminatorKey && fields[path] != null && !fields[path]) {
delete fields[path];
return;
}
Expand Down
11 changes: 11 additions & 0 deletions test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4170,4 +4170,15 @@ describe('Query', function() {
await q.exec();
assert.equal(q.op, 'findOneAndReplace');
});

it('allows deselecting discriminator key (gh-13679)', async function() {
const testSchema = new Schema({ name: String });
const Test = db.model('Test', testSchema);
const Test2 = Test.discriminator('Test2', new Schema({ test: String }));

const { _id } = await Test2.create({ name: 'test1', test: 'test2' });
const { name, __t } = await Test.findById(_id).select({ __t: 0 });
assert.strictEqual(name, 'test1');
assert.strictEqual(__t, undefined);
});
});

0 comments on commit 48edaf1

Please sign in to comment.