如何在不删除子文档数据的情况下更新文档

时间:2019-05-12 09:04:39

标签: node.js mongodb mongoose

我正在将NODE-js与猫鼬一起使用。当我尝试使用子文档更新具有数组属性的文档时,将从文档中删除没有值的子文档键。 如何在不删除子文档字段的情况下进行更新

更新前的文档:

{
    "Code": 0,
    "Data": {
        "_id": "5cd11d8edc462b23fc1557a9",
        "Teams": [
            {
                "_id": "5cd11d8edc462b23fc1557ac",
                "GameState": 2,
                "Name" : "test

            }
        ],
        "Name": "Name"
    },
    "Message": ""
}

更新json:

{
    "_id" : "5cd11d8edc462b23fc1557a9",
    "Teams" : [
        {
            "_id" :"5cd11d8edc462b23fc1557ac",
            "GameState" : 2
        }
        ]           
}

更新后的文档:

{
    "Code": 0,
    "Data": {
        "_id": "5cd11d8edc462b23fc1557a9",
        "Teams": [
            {
                "_id": "5cd11d8edc462b23fc1557ac",
                "GameState": 2
            }
        ],
        "Name": "Name"
    },
    "Message": ""
}

更新文档:

let group =  await Group.findOneAndUpdate({_id : req.body._id},{ $set: req.body},{ new: true });

0 个答案:

没有答案