Mongoose findOneAndUpdate: update an object in an array of objects

前端 未结 2 1054
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-07 11:45

I have the exact same issue as described in this thread (hence the similar title): Mongoose findOneAndUpdate -- updating an object inside an array of objects

Given t

相关标签:
2条回答
  • I had the same problem, so I just delete $set from .findOneAndUpdate

    Example:

    function updateFood (userId) {
      SavedFoods.findOneAndUpdate(
        {
          id: userId,
          'list.id': 0
        },
        {
          {'list.$.name': 'Something else'}
        },
        null,
        (err) => {
          if (err) {
            console.log('Error:', err)
          } else {
            console.log('Updated', userId)
          }
          process.exit(0)
        }
      )
    }
    

    Works for me.

    0 讨论(0)
  • 2021-01-07 12:31

    I think you mismatched id and user field

    Try to change id with user

    SavedFoods.findOneAndUpdate(
        {
          user: userId,
          'list.id': 0
        }
    )
    
    0 讨论(0)
提交回复
热议问题