mongoDB : updating an objects using dot notation (multi-level object)

浪子不回头ぞ 提交于 2019-12-06 09:39:30

This won't work because groups is an array, but you're referencing a property in it as if it were an object. You could access things like groups.0.members or groups.1.members because these refer to specific items in the array, but the groups object itself doesn't have a members attribute to append to.

I'm not totally sure what your update needs to do exactly, but you could add a filter on groups.groupName to your query, and then do $push with groups.$.members to append a member only to the group that matched.

http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator

It's because your user model can have multiple groups so you need to match a group in the filter and use the positional operator $ in the update. So your update would be something like this (untested):

db.collection.update({
    userId: "1",
    "groups.groupName": "testGroup" // match against the group here
}, {
    $push: {
        "groups.$.members": { // notice the use of the $ here
            membersDetails: {
                userId: "2",
                faceBookId: "54321"
            },
            memberFirstName: "UserB",
            memberLastName: "UserBLastName"
        }
    }
});

When you have an array of values, the positional operator basically says "at the position of the matched value in the array".

However, you can only do this one level of arrays deep, so you wouldn't be able to match a specific member in a specific group, for example - so you may want to break up your document into a number of simpler collections if you're going to need to do this sort of thing.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!