I have a table with data like
{
_id: ....
Name
...
"RoomStatusDetails": [
{
"StatusEntryId": ObjectId("5bd6ea81d2ccda0a780054da"),
"RoomId": "78163a07-76db-83c1-5c22-0749fab73251",
"CurrentStatus": ObjectId("5bd17295d2ccda11f0007765"),
"StartDate": ISODate("2018-10-09T22:00:00.0Z"),
"Notes": "Notes for in service",
"Discrepancy": "Discrepency",
"Waiver": "Waiver",
"TFlight": "T Flight",
"IsActive": "Inactive"
},
{
"StatusEntryId": ObjectId("5bd6ecf3d2ccda0a780054db"),
"RoomId": "78163a07-76db-83c1-5c22-0749fab73251",
"CurrentStatus": ObjectId("5bd17295d2ccda11f0007766"),
"StartDate": ISODate("2018-10-16T22:00:00.0Z"),
"Notes": "Out of service",
"Discrepancy": "",
"Waiver": "",
"TFlight": "",
"IsActive": "Active"
},
...
}
I have written below lines of code for updating IsActive field to "Inactive" on the basis of RoomId
$this->collection->updateOne(array('_id' => new MongoDB\BSON\ObjectID($this->id), "RoomStatusDetails" =>
array('$elemMatch' => array("RoomId" => $this->RoomId))),
array('$set' => array("RoomStatusDetails.$.IsActive" => 'Inactive')), array("multi" => true, "upsert" => false));
The above code is not updating all the IsActive field. Please help!!!
For updating all matched embedded documents you should use $[] because $
refer the first position of matched embedded document.
the positional $ operator acts as a placeholder for the first element that matches the query document