gpt4 book ai didi

mongodb - 在mongod php中更新数组中的多个元素

转载 作者:可可西里 更新时间:2023-11-01 10:38:36 25 4
gpt4 key购买 nike

我有这样的文件

       {
"_id": ObjectId("5b570181d2ccda05180055ed"),
"VehicleNumber": "7656hhh",
"DriverDetails": [
{
"DriverEntryId": "5016cfe5-e5ca-f9e1-35c6-1ab1d09230fb",
"DriverId": ObjectId("5b488d70d2ccda01840078e6"),
"StartDate": ISODate("2018-07-17T22:00:00.0Z"),
"EndDate": ISODate("2018-07-27T22:00:00.0Z"),
"Status" : "Active"
},
{
"DriverEntryId": "772cb165-33e3-6d92-ff04-007785e52a84",
"DriverId": ObjectId("5b507122d2ccda04580006ca"),
"StartDate": ISODate("2018-07-02T22:00:00.0Z"),
"EndDate": ISODate("2018-07-18T22:00:00.0Z"),
"Status": "Active"
},
{
"DriverEntryId": "12d1fa8a-9987-2c27-119e-80dd434c0534",
"DriverId": ObjectId("5b488d70d2ccda01840078e6"),
"StartDate": ISODate("2018-06-24T22:00:00.0Z"),
"EndDate": ISODate("2018-07-31T22:00:00.0Z"),
"Status": "Active"
}
],
..
}

我想为所有嵌入式文档设置“非事件”状态。我试过下面的代码行。它不工作...请帮忙!!!

      $this->collection->updateOne(
array('_id' => new MongoDB\BSON\ObjectID($this->id)
),
array('$set' =>
array('DriverDetails.$.Status' => 'Inactive'
)
)
);

最佳答案

您需要使用 $[]用于更新数组中每个元素的所有位置运算符

$this->collection->updateOne(
array('_id' => new MongoDB\BSON\ObjectID($this->id)),
array('$set' => array('DriverDetails.$[].Status' => 'Inactive'))
)

相当于JS

db.collection.update(
{ "_id": "546djdhu99wijfejf9euf94ef" },
{ "$set": { "DriverDetails.$[].Status": "Inactive" }}
)

关于mongodb - 在mongod php中更新数组中的多个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51535537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com