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

Model.updateOne() and arrayFilters fails to update embedded document array fields when overwriting embedded discriminator key #15051

Open
2 tasks done
lcrosetto opened this issue Nov 20, 2024 · 0 comments

Comments

@lcrosetto
Copy link

lcrosetto commented Nov 20, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.8.2

Node.js version

20.18.0

MongoDB server version

8.0.1

Typescript version (if applicable)

No response

Description

When using Model.updateOne() and arrayFilters to overwrite the discriminator key in an embedded document array with schema discriminators (and setting the overwriteDiscriminatorKey option), the discriminator key is updated, but fields unique to the discriminator schema fail to be updated. I suspect this is a problem with determining the schema from the update object, possibly related to the getEmbeddedDiscriminatorPath() helper.

Steps to Reproduce

The following script will fail to update field2:

      const conn = mongoose.createConnection(..);

      const embedDiscriminatorSchema = new mongoose.Schema({
        field1: String,
      });
      const embedDiscriminatorSchema2 = new mongoose.Schema({
        field2: String,
      });
      const embedSchema = new mongoose.Schema({
        field: String,
        key: String,
      }, {discriminatorKey: 'key'});
      embedSchema.discriminator('Type1', embedDiscriminatorSchema);
      embedSchema.discriminator('Type2', embedDiscriminatorSchema2);

      const testSchema = new mongoose.Schema({
        testArray: [embedSchema],
      });

      const TestModel = conn.model('Test', testSchema);
      const test = new TestModel({
        testArray: [{
          key: 'Type1',
          field: 'field',
          field1: 'field1',
        }],
      });

      const r1 = await test.save();
      assert.equal(r1.testArray[0].field1, 'field1');

      const field2update = 'field2 update';
      await TestModel.updateOne(
        { _id: r1._id },
        {
          $set: {
            'testArray.$[element].key': 'Type2',
            'testArray.$[element].field2': field2update,
          },
        },
        {
          arrayFilters: [
            {
              'element._id': r1.testArray[0]._id,
            },
          ],
          overwriteDiscriminatorKey: true,
        },
      );

      const r2 = await TestModel.findById(r1._id);
      assert.equal(r2.testArray[0].key, 'Type2');
      assert.equal(r2.testArray[0].field2, field2update);

      conn.deleteModel('Test');

Expected Behavior

Model.updateOne() should update the fields from the discriminator (key) set in the update, if overwriteDiscriminatorKey is set to true. Probably the schema would be found using the discriminator key in the update:

'testArray.$[element].key': 'Type2',

@lcrosetto lcrosetto changed the title Model.updateOne() fails to update embedded fields when overwriting discriminator key Model.updateOne() and arrayFilters fails to update embedded document array fields when overwriting embedded discriminator key Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant