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

Updating nested discriminators by dot notation #8378

Closed
nhitchins opened this issue Nov 25, 2019 · 0 comments
Closed

Updating nested discriminators by dot notation #8378

nhitchins opened this issue Nov 25, 2019 · 0 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@nhitchins
Copy link

Do you want to request a feature or report a bug?
Bug or extension of current feature?

What is the current behavior?
Updating a nested discriminator by dot notation drops any attributes not defined in base schema even if discriminator key is provided in the update.

If the current behavior is a bug, please provide the steps to reproduce.

const shapeSchema = Schema({ name: String }, { discriminatorKey: 'kind' });
const schema = Schema({ shape: shapeSchema });

schema.path('shape').discriminator('Circle', Schema({ radius: String, color: String }));
schema.path('shape').discriminator('Square', Schema({ side: Number, color: String }));

const MyModel = mongoose.model('ShapeTest', schema);

doc = await MyModel.create({
  shape: {
    kind: 'Circle',
    name: 'circle test',
    radius: 5,
    color: 'red'
  }
});
 
const updatedDoc = await MyModel.findByIdAndUpdate(
  doc._id,
  {
      $set: {
         'shape.kind': 'Circle',
         'shape.name': 'updated name',
         'shape.radius': 10
      }
   }
);

updatedDoc.shape.name; // updated name
updatedDoc.shape.radius; // 5

What is the expected behavior?
The update will cast, (validate when runValidators=true) and update 'shape.kind' and 'shape.name' but drops 'shape.radius' which is defined in the discriminator schema. When updates use dot notation and path is a nested (or array) discriminator then should use discriminator key provided in the updates (or fallback to existing doc discriminatorKey value?) for correct casting. Currently the only way to set 'shape.radius' is to set strict=false (which defeats the purpose of defining a schema) or first retrieve the doc, apply the partial updates and .save(). Atomic operations are required.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js 10.16.3
Mongoose 5.7.12
MongoDB 4.0.3

@vkarpov15 vkarpov15 added this to the 5.7.14 milestone Dec 1, 2019
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Dec 1, 2019
vkarpov15 added a commit that referenced this issue Dec 5, 2019
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Dec 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

2 participants