-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
updateOne with embedded discriminators in strict mode throws with $push, but not with $set #9977
Comments
I fixed the |
Thank you, but there is still an issue with the positional operators It seems that mongoose does not take the Consider this: await Quiz.updateOne(
{
questions: {
$elemMatch: {
_id: id2,
// if we don't include `question_type`, mongoose will not
// allow the update
question_type: "mcq",
},
},
},
{
$push: {
// but note that here we are updating _all_ the questions, even if
// the `question_type` is not `mcq`.
// mongoose does not check this and will send the update although
// it should not.
"questions.$[].choices": {
choice_text: "choice 3",
is_correct: false,
},
},
}
); Also the opposite may happen, although the following should be a valid await Quiz.updateOne(
{
questions: {
$elemMatch: {
_id: id2,
// note: not including the `question_type` here
},
},
},
{
$push: {
"questions.$[q].choices": {
choice_text: "choice 3",
is_correct: false,
},
},
},
{
// instead we include it as part of the arrayFilters
arrayFilters: [{ "q.question_type": "mcq" }],
}
); This is tested using mongoose |
Do you want to request a feature or report a bug?
Report a bug
What is the current behavior?
Using
updateOne
to update an array field inside an embedded array document with discriminators is not consistent.If the current behavior is a bug, please provide the steps to reproduce.
Create a quiz
Try to update the second question:
What is the expected behavior?
I think at least
$push
should not throw because it's not different from$set
Maybe the filtered positional operator (
$[q]
) is different, because mongoose needs to also check thearrayFilters
to be sure that the question that will be updated has the correct discriminator value (mcq
). (I think that would be a new feature)$set
should update the timestamps (this may be a different issue)What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
The text was updated successfully, but these errors were encountered: