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

4
  • None of those values are valid for ObjectId Commented Mar 18, 2015 at 8:32
  • yes those _id are not valid ObjectId, I just make it short and more readable for the sample object Commented Mar 18, 2015 at 8: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. Commented Mar 18, 2015 at 8:40
  • thank you for the advice, I will keep this in mind Commented Mar 18, 2015 at 8:46

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.