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
_idare not valid ObjectId, I just make it short and more readable for the sample object_idvalues. But in the context of your question you need them, so you could have just presented them as plain strings, which are still valid. You want good answers, then make it as simple as possible for others to use your example data. Just a tip for future reference.