I have this document:
{
"id" : "001",
"items" : {
"persons" : [
{
"city" : "London",
"color" : "red",
"status" : "1"
},
{
"city" : "Paris",
"color" : "blue",
"status" : "0"
}
],
"animals" : [
{
"city" : "Luton",
"color" : "red",
"status" : "1"
},
{
"city" : "London",
"color" : "red",
"status" : "0"
}
]
}
In java, or at least direcly in mongo, I need to update the status of persons and animals to a new value for each item in those arrays who meets the criteria of city and/or color from input.
Status is obligatory, city and color are not obligatory.
Ex:
input city=London, color=red and status=9 will result in:
{
"id" : "001",
"items" : {
"persons" : [
{
"city" : "London",
"color" : "red",
"status" : "9"
},
{
"city" : "Paris",
"color" : "blue",
"status" : "0"
}
],
"animals" : [
{
"city" : "Luton",
"color" : "red",
"status" : "1"
},
{
"city" : "London",
"color" : "red",
"status" : "9"
}
]
}
input color=red and status=7 will result in:
{
"id" : "001",
"items" : {
"persons" : [
{
"city" : "London",
"color" : "red",
"status" : "7"
},
{
"city" : "Paris",
"color" : "blue",
"status" : "0"
}
],
"animals" : [
{
"city" : "Luton",
"color" : "red",
"status" : "7"
},
{
"city" : "London",
"color" : "red",
"status" : "7"
}
]
}
I tried a lot of things, but nothing works. If somebody can came with a solution is great, but I will be satisfied with a piece of advice to guide me to solve this matter.
2
Answers
From your question i just understood that you want to update status of those 2 objects
persons
,animals
by criteria ofor
.I hope it will help you
for only color
ex- https://mongoplayground.net/p/UTLAVWQ884V
for both color and city
ex- https://mongoplayground.net/p/qRdi2c1VDQg