Suppose I have the following document in the persons
collection:
{
"_id" : ObjectId("54e42f8c1de15b081125318c"),
"name" : "Mike",
"family" : {
"mother" : "Alice",
"father" : "Bob",
"brothers" : [
{
"_id" : ObjectId("54e42f8c1de15b081125318d"),
"name" : "David"
}, {
"_id" : ObjectId("54e42f8c1de15b081125318e"),
"name" : "Jason"
}
],
"sisters" : [
{ // target subdocument
"_id" : ObjectId("54e42f8c1de15b081125318f"),
"name" : "Tifany"
}, {
"_id" : ObjectId("54e42f8c1de15b0811253190"),
"name" : "Samantha"
}
]
}
}
How can I update the target subdocument with {"name" : "Tiffany"}
by the condition {"_id" : ObjectId("54e42f8c1de15b081125318f")}
without knowing the path (i.e. "family.sisters"
)
==============================================================
I can do something like this if I know the path:
db.persons.update({"family.sisters._id" : ObjectId("54e42f8c1de15b081125318f")}, {"family.sisters.$.name" : "Tiffany"})
==============================================================
Sorry, those answers in the duplicate question may not be what I want