更新子文档数组在PHP Mongo



我正在尝试更新子文档数组在PHP Mongo

$result = $collection->updateMany(
[
'_id' => new MongoDBBSONObjectID('61613dcd437b996bc227ffe2'),
'messages.type' => 'test'
],
['$set' => ['messages.$[m].direction' => '999']],
[
'multi' => true,
'arrayFilters' => [['m.type'=> 'test']]
]
);

# # # #更新。数组是

[{"_id"{"oid&quot美元:6163年"f2c9fc197a54f03ca8c8"},"id": 7,},"messages":{"1":{"type"1","time":"19.04.2020 09:14:42","message":"这是testmsg"}},"ssg":"ruther"}]

另外,我如何更新整个子文档索引1,2,3..

使用$[]arrayFilters

db.collection.update({
"_id": ObjectId("61603458c08ad41af13bf352"),
"messages.type": "05.04.2020 03:27:23"
},
{
$set: {
"messages.$[m].direction": "999"
}
},
{
multi: true,
arrayFilters: [
{
"m.type": "05.04.2020 03:27:23"
}
]
})

如果你想用新的Message数组替换原来的Message数组

$set: {
"messages.$[m]": {
"type": "02.06.2020 19:27:23",
"message": "This is a 3 Msg",
"direction": "0"
}
}

mongoplayground

相关内容

  • 没有找到相关文章

最新更新