这一定很简单,但我似乎无法弄明白......假设我有一个集合users
,这是该集合中的第一个项目:
{
"_id" : ObjectId("4d8653c027d02a6437bc89ca"),
"name" : "Oscar Godson",
"email" : "oscargodson@xxx.com",
"password" : "xxxxxx",
"tasks" : [
{
"task" : "pick up stuff",
"comment" : "the one stuff over there"
},
{
"task" : "do more stuff",
"comment" : "lots and lots of stuff"
}
] }
如何使用MongoDB的PHP驱动程序然后将另一个“任务”添加到集合中此项目中的“任务”数组
答案 0 :(得分:14)
使用Mongo的$push
操作:
$new_task = array(
"task" => "do even more stuff",
"comment" => "this is the new task added"
);
$collection->update(
array("_id" => ObjectId("4d8653c027d02a6437bc89ca")),
array('$push' => array("tasks" => $new_task))
);