如何在mongodb中更新数组项的对象属性值?

时间:2018-03-31 20:03:35

标签: node.js database mongodb

{
    "_id": "chat",
    "toList": [
        {
            "_id": "person 1",
            "messages": {
                "_id": "1",
                "message": "Message from person 1"
            }
        },
        {
            "_id": "Person 2",
            "messages": [
                {
                    "_id": "1",
                    "message": "Message from person 2"
                },
                {
                    "_id": "2",
                    "message": "new message"
                }
            ]
        }
    ]
}

嗨,这是我的db客户文档。我想更改(更新)消息属性值从“新消息”“来自人2的新消息” messages._id“:”{2}“”toList._id“:”人2“。请帮我找到合适的解决方案。

我的代码:

db.customers.update({"_id":"Chat", "toList._id":"Person 2", "toList.messages._id":"2"},{$set:{"toList.$.messages.message":"new message from person 2"}}, function(err, res)
{
    console.log(res);
});

0 个答案:

没有答案