简体   繁体   English

使用 mongoose 更新文档(更新文档属性的属性)

[英]Update a document with mongoose (update property of document property)

I would like to update the document of a user with form inputs using mongoose.我想使用 mongoose 使用表单输入更新用户的文档。 Inside the user document I would like to access the personal info section (and in this case its fullName property) to update it with the form's data.在用户文档中,我想访问个人信息部分(在本例中是其 fullName 属性)以使用表单数据更新它。 I tried with personalInfo.fullName in the mongoose update function, but this doesn't seem to work.我在猫鼬更新功能中尝试使用personalInfo.fullName,但这似乎不起作用。 Can anyone fix this?任何人都可以解决这个问题吗?

这是用户文档的示例

router.post('/personalInfo', function (req, res, next) {


    User.update({username: req.user.username}, {$set: { personalInfo.fullName: req.body.fullName}}, function (err, user) {
        if (err) throw error
        console.log(user);
        console.log("update user complete")
    })
});

Try adding quotes around "personalInfo.fullName" :尝试在"personalInfo.fullName"周围添加引号:

User.update({
  username: req.user.username
}, {
  $set: { 
    "personalInfo.fullName": req.body.fullName
  }
}, function (err, user) {
    if (err) throw err
    console.log(user)
    console.log("update user complete")
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM