Update array object based on id?

前端 未结 2 1143
有刺的猬
有刺的猬 2020-12-11 09:17

I\'m having a bit of a mongo issue. I was wondering if there was a way to do the following in a mongo console command rather then multiple find and update

2条回答
  •  伪装坚强ぢ
    2020-12-11 09:51

    You can use the $ positional operator to do this:

    db.soup.update(
        {_id: ObjectId("50b429ba0e27b508d854483e"), 'array.id': '2'}, 
        {$set: {'array.$.letter': 'c'}})
    

    The $ in the update object acts as a placeholder for the first element of array to match the query selector.

提交回复
热议问题