我在mongo中有这个文档:
{
"_id": ObjectId("4d0b9c7a8b012fe287547157"),
"done_by": ["1"]
}
并且我想向"done_by“字段添加另一个值,因此我期望的文档将是::
{
"_id": ObjectId("4d0b9c7a8b012fe287547157"),
"done_by": ["1","2","3"]
}
我试试这个:
$conn = new Mongo();
$q = $conn->server->gameQueue;
$id = new MongoId("4d0b9c7a8b012fe287547157");
$q->update(array("_id"=>$id),array('$push' => array("done_by","2")));
但是什么都没发生,有人知道怎么做吗?
发布于 2011-01-10 06:13:00
$filter = array('_id'=>$id));
$update = array('$push'=>array('done_by'=>'2'));
$q->update($filter,$update);
发布于 2011-01-10 05:14:21
$push => array('done_by' => '2')
manual是这么说的:{ $push : { field : value } }
发布于 2018-02-05 13:05:28
$filter = array('_id'=>$id));
$update = array('$addToSet'=>array('done_by'=>'2'));
$q->update($filter,$update);
当您需要更新时,使用$addToSet
,这样您就可以避免重复插入,这会导致多个条目。
https://stackoverflow.com/questions/4638368
复制相似问题