Ex: animals collection:
[
{
"animal_id": "cat",
"foo": []
},
{
"animal_id": "dog",
"foo": [
{
"val": "ten",
"fields": [
{
"f1": "v1"
}
]
},
{
"val": "ele",
"fields": [
{ "f2": "v2" },
// { "f3": "v3" } add this here
]
}
]
}
]update qury:
db.animals.updateOne(
{
"animal_id": "dog",
"foo.val": "ele"
},
{
$push: {
"foo.$.fields": { "f3": "v3" }
}
}
)
Ex: fruit collection
[
{
"country": "India",
"fruits": {
"mainFruits": [
"mango",
"apple"
]
}
},
"country": "USA",
"fruits": {
"mainFruits": [
"banana",
"apple" // <--- remove this
]
}
]updaete query:
db.fruit.updateOne(
{ country: "USA" },
{ $pull: { "fruits.mainFruits": "apple" } }
);