MongoDB中更新元素,应该怎么改?

{
    "_id" : 3,
    "label" : "径前肌",
    "files" : [ 
        {
            "id" : "8eb7d8fe-de08-446d-802e-3a25743fa137",
            "filename" : "ybl_20190605_1559700070044.kfb",
            "info" : "/images/ybl_20190605_1559700070044.kfb_labelinfo.jpg",
            "preview" : "/images/ybl_20190605_1559700070044.kfb_priview.jpg",
            "viewer" : "/viewer?name=ybl_20190605_1559700070044d.kfb",
            "dzi" : "/images/ybl_20190605_1559700070044.kfb.dzi",
            "scale" : "/images/info/ybl_20190605_1559700070044.kfb",
            "saveUser" : "JKZwSny9vvHDFsTYD",
            "saveHospital" : "上海市闵行区浦江镇浦江卫生院",
            "createAt" : 1559700328326.0,
            "updateAt" : 1559700328326.0,
            "status" : 0,
            "annotationsUsers" : []
        }
    ]
}

这是当前mongo中的数据结构现在想在annotationsUsers这个数组中存入以下对象,应该怎么做,根据_id和id了

        {
          "createAt": "",
          "saveUser": "",
          "result": "",
          "annotations": [ 
            {
              "image": "",
              "createAt": "",
              "points": [],
            }

现在这样写的

dbo.collection('AnnotationFileGroups').update({_id:groupId},{id:fileId}, {$addToSet: {annotationsUsers: newFile}}, e => {
            if(e) throw e;
            res.json({
                code: 200,
                data: {
                    id: newFile.saveUser,
                },
            });

现在存储完结构是错误的,应该怎么改?

阅读 1.9k
1 个回答
伪代码
dbo.collection('AnnotationFileGroups').update(
    {_id:groupId},{files.id:fileId}, 
    {'$set': {'files.$.annotationsUsers': [你的数据]}}
)

clipboard.png

clipboard.png

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进