I have following document on which the update needs to be done.
{
"_id": "Colorcode_1",
"Combination": [
{
"color": [
{
"mixture": [
"Red",
"Green"
]
}
],
"code": "Maroon"
},
{
"color": [
{
"mixture": [
"Yellow",
"Green"
]
}
],
"code": "Light Green"
}
]
}
Now what I need to do is to update the document by adding the value "Blue" in the "mixture" field where "code" is "Maroon". Something like this. This needs to be done using $addToSet
{
"_id": "Colorcode_1",
"Combination": [
{
"color": [
{
"mixture": [
"Red",
"Green",
"Blue"
]
}
],
"code": "Maroon"
},
{
"color": [
{
"mixture": [
"Yellow",
"Green"
]
}
],
"code": "Light Green"
}
]
}
Any help regarding this would be highly helpful.
2
Answers
I found this update difficult because of the data model, and I’m hoping you’ll get a better/simpler answer.
Anyway, here’s one way you could do it. I would test this on more/different data to insure it’s correct.
Try it on mongoplayground.net.
Here is option with arrayFilters:
Explained:
playground