在猫鼬中通过关系_id编辑嵌套值

时间:2019-06-21 06:35:33

标签: node.js mongodb express mongoose mongoose-schema

你好,我在数组中有关系_id。对象内容在其他集合中。我只能在数组中查看Object("_id")

我要编辑那些_ids的对象。如何使用关系_ids ??

进行编辑

我有这样的数据响应

{
"admins": {
            "users": [
                "5d089739d8aae228d7f10c1e" //<------ edit this id object
            ],
            "email": "jertacomlu@desoz.com",
            "password": "$2a$10$C/DsmoHK57vIUt7g4wRsYulHLigmc1wGLObwu2/qkFkSeYp28bAOy",
        },
"_id": "5d089264d8aae228d7f10c1d",
"companyName":"Company 1",
"__v": 0
}

因此,此ID存储在其他集合名称Userr中。

对象是这样的:-

[  {
     "_id": "5d089739d8aae228d7f10c1e",
     "email": "jertacomlu@desoz.com",
     "password": "$2a$10$0Slx3yFeb7UD0qep.TWSI.JpQGl0CzlGhNJ162JtA5Uvt6osyrIl.",
     "firstName": "Sima",
     "lastName": "Cezo",
     "phoneNumber": "8877123456",
      "__v": 0
    }
 ]

我要编辑这些字段。如何使用关系_id

编辑:-

我的父控制器

var admin = new Admin();
    admin.companyName = req.body.companyName;
    admin.admins = {
                    email : req.body.email,
                    password: req.body.password, 
                    role : "admin",
                    verified :"false",
                    active: "OFF",
                    users : []
    };

2 个答案:

答案 0 :(得分:0)

tf.Tensor(
[ 14053.483   25004.557  250343.36    66548.234    6653.863    3470.502
   5318.3926 164206.19    19914.338    1951.0701   3559.3235   7248.4717
   5984.786    7902.9004 133984.66    41497.473   25010.273   22232.062
  26451.926   66250.39     6497.735 ], shape=(21,), dtype=float32)

答案 1 :(得分:0)

您必须在模型用户处创建方法,例如要编辑电子邮件的示例:

User.edit=async function (id,newEmail){
let user = await User.findOne({_id:id}).exec();
user.email=newEmail;
await user.save();
}

管理员中,

...
let admin = await Admin.findOne({_id:id}).exec();
await User.edit(admin.users[0])
...