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
Support updates that filter on multiple discriminator keys with $in
#7843
Comments
A question in regards to this. As can be seen, the the __v is not updated with an updateOne(), but mongoose is able to handle the timestamp. As it is able to handle the timestamp, would it also not be possible to update the version? Or is this another bug? |
@thernstig re: And your original bug report is also expected behavior. When you do await Test.updateOne(
{ label: 'label 1' },
{ label: 'NEW LABEL' }
); Mongoose has no way of knowing what type const doc = await TestChild.updateOne(
{ label: 'label 1' },
{ label: 'NEW LABEL' }
); |
Is this supposed to work then? const doc = await Test.updateOne(
{ label: 'label 1', kind: 'testchild'},
{ label: 'NEW LABEL' }
); |
@thernstig we'll add support for that, that doesn't work currently. |
Another issue with the above which is directly related to #6087 is that you can not update the discriminatorKey value via the discriminator model when doing findByIdAndUpdate This updates the discriminator key but not the label attribute
This only works when the existing doc has kind set to 'testchild'
This means that you can not change both the discriminatorKey value and set attributes based on the new discriminator model in a single update. Possibly a good reason against attaching discriminator keys to queries automatically? |
@nhitchins I'm not sure how your comments are related to this issue, can you please clarify? |
Hi, Sorry for reviving an old issue, regarding thernstig's last comment, is there a way to do it for multiple types like so? const doc = await Test.updateMany(
{ label: 'label 1', kind: { $in: ['testchild1', 'testchild2'] } },
{ label: 'NEW LABEL' }
); |
@Marsup unfortunately not currently. You would need to do two separate await Test.updateMany(
{ label: 'label 1', kind: 'testchild1' },
{ label: 'NEW LABEL' }
);
await Test.updateMany(
{ label: 'label 1', kind: 'testchild2' },
{ label: 'NEW LABEL' }
); |
@vkarpov15 multiple calls would not be good if we have many values for |
@piyushk96 the only workaround we have right now would be to use const doc = await Test.updateMany(
{ label: 'label 1', kind: { $in: ['testchild1', 'testchild2'] } },
{ label: 'NEW LABEL' },
{ strict: false }
); That skips type checking for the Another potential alternative would be to create a separate model with just the testchild1 and testchild2 discriminators. So a model that always has |
$in
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
Using updateOne() on discriminated models properties does not work. Here is a repro script.
If the current behavior is a bug, please provide the steps to reproduce.
Output
What is the expected behavior?
I expected it to update label to the new label. Note that if you change the update to e.g. this, it works:
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Moongose 5.5.2
Mongodb 3.6.3
Node.js 10.13.0
The text was updated successfully, but these errors were encountered: