更新多个嵌入文档不起作用

Updating multiple embedded document not working

我有一个 table 数据如

 {
  _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 字段。请帮忙!!!

所有匹配的嵌入文档:

要更新所有匹配的嵌入文档,您应该使用$[] because $参考匹配嵌入文档的第一个位置。

the positional $ operator acts as a placeholder for the first element that matches the query document

您必须使用 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))
);