我正在尝试更新文档的子文档,如下面的代码行
$bulkbatch = new MongoDB\Driver\BulkWrite(['ordered' => true]);
$subDocumentStatus = array(
"Status" => array(
"CurrentStatus" => $this->EmployeeStatus,
"StatusDate" => new MongoDB\BSON\UTCDateTime(strtotime($this->EmployeeStatusDate)*1000),
"IsActive" => $IsActive
)
);
$bulkbatch->update(
array(
'_id' => new MongoDB\BSON\ObjectID($this->id)
),
array(
'$addToSet' => $subDocumentStatus
),
[
'multi' => true,
'upsert' => false
]
);
$this->manager->executeBulkWrite($this->collection, $bulkbatch);
我有像这样的文件
{
"EmployeeNumber": "123456",
"Instructor": NumberInt(1),
"Department": ObjectId("5a8173e6d2ccda13240015a4"),
"FirstName": "Faiza",
"MiddleName": "Yousuf",
"Status": [
{
"CurrentStatus": "Joined",
"StatusDate": ISODate("2018-02-28T23:00:00.0Z"),
"IsActive": NumberInt(1)
}
],
...
...
}
如果 $this->EmployeeStatus 包含“Joined”并且 $this->EmployeeStatusDate 包含一些不同的值,例如上面的文档它包含“2019-05-28T23:00:00.0Z”(除了以前的值),那么它应该被更新。
上面的代码插入另一个子文档,其值如下
"Status": [
{
"CurrentStatus": "Joined",
"StatusDate": ISODate("2018-02-28T23:00:00.0Z"),
"IsActive": NumberInt(1)
},
{
"CurrentStatus": "Joined",
"StatusDate": ISODate("2019-05-28T23:00:00.0Z"),
"IsActive": NumberInt(1)
}
],
...
我希望在 Joined 的情况下应该更新现有的子文档。
请帮忙!!!