mongoDB:使用点表示法更新对象(多级对象) [英] mongoDB : updating an objects using dot notation (multi-level object)

查看:155
本文介绍了mongoDB:使用点表示法更新对象(多级对象)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下形式的对象:

I have an object with the following form :

{
    "_id": ObjectId("4fa43f4d1cf26a6a8952adf1"),
    "userId": "1",
    "facebookId": "1234",
    "groups": [{
        "groupName": "testGroup",
        "members": [{
            "memberFirstName": "userFirstName",
            "memberLastName": "userLastName",
            "memberDetails": {
                "userId": "1",
                "faceBookId": "1234"
            }
        }]
    }]
}

这是一个为每个用户保存的集合-它的组, 每个小组都包含小组成员... 因此,组"键是一个数组(或列表),成员"键也是如此. (每个用户可以有多个组,每个组有多个组成员.)

It's a collection that holds for each user - its groups, with each group containing the group members... So the "groups" key is an array (or list), and so is the "members" key. (each user could have multiple groups, each group has multiple group members).

我很难用新的组成员更新对象, 我想做的是:

I'm having a hard time updating the object with a new group member, what I'm trying to do is :

db.collection.update({
    userId: "1"
}, {
    $push: {
        "groups.members": {
            membersDetails: {
                userId: "2",
                faceBookId: "54321"
            },
            memberFirstName: "UserB",
            memberLastName: "UserBLastName"
        }
    }
});

但这似乎不起作用.

遇到以下错误:

can't append to array using string field name [members]

我也在尝试使用Java驱动程序,但这似乎也不起作用.

I'm also trying to work with the Java driver, but that doesn't seem to work as well.

DBObject query = new BasicDBObject("userId", "1");

DBObject newMemberDetailsObj = new BasicDBObject();
newMemberDetailsObj.put("userId", "2");
newMemberDetailsObj.put("faceBookId", "54321");

DBObject newMemberObj = new BasicDBObject();
newMemberObj.put("memberFirstName", "userB");
newMemberObj.put("memberLastName", "UserBLastName");
newMemberObj.put("memberDetails", newMemberDetailsObj );

DBObject update = new BasicDBObject("$push", new BasicDBObject("members", newMemberObj));
update.put("groupName", "testGroup");       

DBObject maintain = new BasicDBObject("$push", new BasicDBObject("groups", update));

WriteResult newWr = coll.update(query, maintain);

推荐答案

这是因为您的用户模型可以具有多个组,因此您需要在过滤器中匹配一个组,并在更新中使用位置运算符$.因此,您的更新将是这样的(未经测试):

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.

这篇关于mongoDB:使用点表示法更新对象(多级对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆