0

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

kit
  • 305
  • 3
  • 13
  • None of those values are valid for [ObjectId](http://docs.mongodb.org/manual/reference/object-id/) – Neil Lunn Mar 18 '15 at 08:32
  • yes those `_id` are not valid ObjectId, I just make it short and more readable for the sample object – kit Mar 18 '15 at 08:36
  • I understand that you might think it is more "readable". But those who you are asking to solve your problems just want to "copy/paste" your data and use it. For more short and readable, remove the `_id` values. 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. – Neil Lunn Mar 18 '15 at 08:40
  • thank you for the advice, I will keep this in mind – kit Mar 18 '15 at 08:46

0 Answers0