Tijl Declerck
Tijl Declerck

Reputation: 105

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. 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. I tried with personalInfo.fullName in the mongoose update function, but this doesn't seem to work. Can anyone fix this?

Here is an example of a user document

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")
    })
});

Upvotes: 2

Views: 6841

Answers (1)

Steve Holgado
Steve Holgado

Reputation: 12071

Try adding quotes around "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")
})

Upvotes: 4

Related Questions