【发布时间】:2016-12-07 08:33:16
【问题描述】:
我正在使用这个 php 库 http://mongodb.github.io/mongo-php-library/
我有一个收藏帖子,在它的一个文档中我添加了多个 cmets,我可以像这样添加新的 cmets
$collection = (new MongoDB\Client)->project->posts;
$id = '5799a81fa60afa2010001838';
$result = $collection->updateOne(
["_id" => new MongoDB\BSON\ObjectID($id)],
['$push' => [
"comments" => ['id'=>1,'name' => 'sohss','comm'=>'I like it']
]
]
);
现在如何更新我尝试过的任何特定评论但没有成功
$updateResult = $collection->updateOne(
['_id' => new MongoDB\BSON\ObjectID($id),'comments'],
['$set' =>
['comments'=>[0]=>
['name'=>'123']
]
]
);
更新子文档的正确语法是什么。
文档结构如下:
{
"_id" : ObjectId("5799a81fa60afa2010001838"),
"title" : "test",
"comments" : [
{
"id" : 3,
"name" : "soh",
"comm" : "I like it"
},
{
"id" : 1,
"name" : "sohss",
"comm" : "I like it"
}
]
}
【问题讨论】:
-
能否显示文档的当前结构以及更新后文档的结构
-
更新后的文档应该是什么样子?
-
我想在子文档中更新“comm”字段:“我喜欢它”我该怎么做
标签: mongodb mongodb-php