我正在尝试向 mongodb 的条目添加评论。
这就是我到目前为止所拥有的
$mongo = new Mongo();
$db = $mongo->comedy;
$collection = $db->cartoons;
$obj = array(
"title" => "football", array('comment' => 'my comment here'),
"author" => "joe"
);
$collection->insert($obj);
产生这个条目
{
"_id": ObjectId("5059fd31ba76883414000001"),
"title": "football",
"0": {
"comment": "my comment here"
},
"author": "joe"
}
我的问题是这是在“足球”条目下嵌套评论的最佳方式吗?还是我应该以不同的方式去做?
这部分似乎不正确
"0": {
"comment": "my comment here"
}
从下面的示例更新
,运行它会出错Fatal error: Call to undefined method MongoDB::update()
$mongo = new Mongo();
$db = $mongo->comedy;
$collection = $db->cartoons;
$mongo->comedy->update(array('title' => 'football'), array(
'$push' => array('comments' => array('content' => 'Yo!', 'author' => $user_id))
));
然后当我运行它时
$mongo = new Mongo();
$db = $mongo->comedy;
$collection = $db->cartoons;
$obj = array(
'$set' => array("title" => "football", "author" => "joe"),
'$push' => array('comments' => array('content' => 'Yo!'))
);
我明白了
{
"_id": ObjectId("505a2493ba76883c08000007"),
"title": "football",
"0": {
"$push": {
"comments": {
"content": "Yo!"
}
}
},
"author": "joe"
}