Mongoose findOneAndUpdate: update an object in an array of objects

前端 未结 2 1074
佛祖请我去吃肉
佛祖请我去吃肉 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条回答
  •  不要未来只要你来
    2021-01-07 12:24

    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.

提交回复
热议问题