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: infer discriminator key if set in $set with overwriteDiscriminatorKey #15243

Merged
merged 1 commit into from
Feb 8, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@
* fix: disallow using $where in match
* perf: cache results from getAllSubdocs() on saveOptions, only loop through known subdoc properties #15055 #15029
* fix(model+query): support overwriteDiscriminatorKey for bulkWrite updateOne and updateMany, allow inferring discriminator key from update #15046 #15040
=======
>>>>>>> 7.x

7.8.3 / 2024-11-26
==================
Expand Down
10 changes: 10 additions & 0 deletions lib/helpers/query/castUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ module.exports = function castUpdate(schema, obj, options, context, filter) {
schema = schema.discriminators[discriminatorValue] ||
(byValue && byValue.schema) ||
schema;
} else if (schema != null &&
options.overwriteDiscriminatorKey &&
obj.$set != null &&
utils.hasUserDefinedProperty(obj.$set, schema.options.discriminatorKey) &&
schema.discriminators != null) {
const discriminatorValue = obj.$set[schema.options.discriminatorKey];
const byValue = getDiscriminatorByValue(context.model.discriminators, discriminatorValue);
schema = schema.discriminators[discriminatorValue] ||
(byValue && byValue.schema) ||
schema;
}

if (options.upsert) {
Expand Down
12 changes: 9 additions & 3 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4174,7 +4174,7 @@ describe('Model', function() {
assert.strictEqual(r2.testArray[0].nonexistentProp, undefined);
});

it('handles overwriteDiscriminatorKey (gh-15040)', async function() {
it('handles overwriteDiscriminatorKey (gh-15218) (gh-15040)', async function() {
const dSchema1 = new mongoose.Schema({
field1: String
});
Expand Down Expand Up @@ -4202,7 +4202,7 @@ describe('Model', function() {
assert.equal(r1.field1, 'field1');
assert.equal(r1.key, type1Key);

const field2 = 'field2';
let field2 = 'field2';
await TestModel.bulkWrite([{
updateOne: {
filter: { _id: r1._id },
Expand All @@ -4214,7 +4214,13 @@ describe('Model', function() {
}
}]);

const r2 = await TestModel.findById(r1._id);
let r2 = await TestModel.findById(r1._id);
assert.equal(r2.key, type2Key);
assert.equal(r2.field2, field2);

field2 = 'field2 updated again';
await TestModel.updateOne({ _id: r1._id }, { $set: { key: type2Key, field2 } }, { overwriteDiscriminatorKey: true });
r2 = await TestModel.findById(r1._id);
assert.equal(r2.key, type2Key);
assert.equal(r2.field2, field2);
});
Expand Down