0

I have a mongoDB collection with multiple users in this format:

_id: 60fd0e98ead1734a1cf735c7

username: "some username"

__v: 0

I can create new users with this database using another POST method. However, I couldn't get it to update. The browser will return the expected result with res.send(result). However, when I check, mongoDB database is never changed. Is there an extra step that mongoDB requires? If the res.send(result) is correct in the browser, where is it reside if not in mongoDB? Here's the code:

const Schema = mongoose.Schema
const usernameSchema = new Schema({username: String,})
const username = mongoose.model("username", usernameSchema)

app.post('/api/users/:_id/exercises', async (req, res) => {

  let user_id = req.body._id
  let query = { _id: user_id }
  let new_username = req.body.username

  username.findOne(query, function (err, result) {
    if (err) {
      console.log(err);
      res.sendStatus(500);
      return
    }
    if (result) {
      result.username = new_username;
      res.send(result)
    }
  })

})
trily84
  • 49
  • 7

2 Answers2

1

As stated in the docs of mongoose ( ) you have to call save() on a document to update it.

document.save(function (err) {
  if (err) return handleError(err);
  // saved!
});

or without loading the document first, you can use findOneAndUpdate

let query = { _id: user_id }
Model.findOneAndUpdate(query, { username: new_username })

https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate

griFlo
  • 2,084
  • 18
  • 28
  • Thank you! I used the findOneAndUpdate and it works, I just have to make sure to use callback per the mongoose documentation. So if I'm still trying to use findOne then update the username within this function, then use a separate username.save to save to mongoDB but it doesn't update the database. Do you know why? – trily84 Aug 05 '21 at 16:43
  • @trily84 I think you have to mark the property as modified: https://stackoverflow.com/a/56679174/4572590 – griFlo Aug 06 '21 at 08:41
0

Using the recommendation above to use findOneAndUpdate per mongoose documentation. This code below works:

let options = {useFindAndModify: false, new:true} 

username.findOneAndUpdate(query, { username: new_username}, options, (err, result) => {
    if (err) return err
    //console.log(result)
    res.send(result)
  })

However, when I revised the code to push an object to database but didn't work for some reason. Here's the new code that didn't do the update. What's wrong with the below code?

const exObj = { 
    description: req.body.description,
    duration: req.body.duration,
    date: req.body.date
  }

  let options = {useFindAndModify: false, new: true} 

  username.findOneAndUpdate(query, { $push: { exercise: exObj } }, options, (err, result) => {
    if (err) return err
    res.send(result)
  })
trily84
  • 49
  • 7
  • I got it eventually. Turned out I have to define the mongoose schema to include those key / value in the object to be added. – trily84 Aug 06 '21 at 03:10