我有一个表,其中包含类似
的数据 {
_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"
},
...
}
我已经编写了以下几行代码,用于根据RoomId将IsActive字段更新为“ Inactive”
$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));
上面的代码未更新所有IsActive字段。请帮忙!!!
答案 0 :(得分:2)
您必须使用arrayFilters来传递房间ID过滤器。
如果没有arrayFilters,它将更新所有[]
“ RoomStatusDetails”数组元素,而不考虑房间ID查询过滤器。
类似
db.col.update(
{"RoomStatusDetails.RoomId" :"78163a07-76db-83c1-5c22-0749fab73251"},
{"$set":{"RoomStatusDetails.$[room].IsActive" : "Inactive"}},
{"arrayFilters":{"room.RoomId":"78163a07-76db-83c1-5c22-0749fab73251"}}
);
在php中
$bulkbatchStatus->update(
array('_id' => new MongoDB\BSON\ObjectID($this->id), "RoomStatusDetails" => array('$elemMatch' => array("RoomId" => $this->RoomId))),
array('$set' => array("RoomStatusDetails.$[room].IsActive" => 'Inactive')),
array("multi" => true, "upsert" => false, "arrayFilters" => array("room.RoomId" => $this->RoomId))
);
答案 1 :(得分:1)