【发布时间】:2021-10-08 12:43:37
【问题描述】:
我正在尝试更新 PHP Mongo 中的子文档数组
$result = $collection->updateMany(
[
'_id' => new MongoDB\BSON\ObjectID('61613dcd437b996bc227ffe2'),
'messages.type' => 'test'
],
['$set' => ['messages.$[m].direction' => '999']],
[
'multi' => true,
'arrayFilters' => [['m.type'=> 'test']]
]
);
####更新。我的数组是
[{"_id":{"$oid":"6163f2c9fc197a54f03ca8c8"},"id":7,},"messages":{"1":{"type":"1","time": "19.04.2020 09:14:42","message":"这是 testmsg"}},"ssg":"ruther"}]
另外,如何使用索引 1、2、3.. 等更新整个子文档?
【问题讨论】:
-
为什么将数值存储为字符串?为什么将
Date值存储为字符串?使用正确的data types,然后您的查询将起作用。 -
日期使用
new MongoDB\BSON\UTCDateTime($value),见php.net/manual/de/class.mongodb-bson-utcdatetime.php
标签: mongodb php-mongodb