MongoDB findAndModify element in array -


i have data below:

{ info:{     id:0000,     name:"iot name",     vendor:"some vendor", }, location:[     {         lat:0,         lng:0,         status:3,         locid:"uniqueid0"     },{         lat:1,         lng:1,         status:0,         locid:"uniqueid1"     },{         lat:2,         lng:2,         status:1,         locid:"uniqueid2"     } ]} 

need example findandmodify or else similar, find in iots collection, location unique id , change status.

e.g find in collection iot, element id=0000 , location locid="uniqueid1" , set status location 2

thanks in advance

apply $set operator $ positional operator in update change set status field location 2. $ positional operator identify correct element in array update without explicitly specifying position of element in array, final update statement should like:

db.iost.update(     {          "info.id": 0000,         "location.locid": "uniqueid1"        },      {         "$set": {             "location.$.status": 2         }     } ) 

Comments