$elemMatch and update

前端 未结 1 1243
孤街浪徒
孤街浪徒 2020-12-11 21:38

I would like to update a subdocument that was fetched using $elemMatch. I\'ve found some posts online but so far I am not able to get it to work. This is what I have:

<
相关标签:
1条回答
  • 2020-12-11 22:20

    You can do this all with a single update call on the model instead of fetching it first with findOne:

    User.update(
      {'addresses._id': address_id},
      {$set: {'addresses.$.street': 'Blah'}},
      function(err, count) { ... });
    

    This uses the positional $ operator in the $set to target just the addresses element that was matched in the query.

    0 讨论(0)
提交回复
热议问题