我试图使用Codeigniter与Codeigniter -mongodb-library/tree/v2,以便与mongo和更新文档与此结构连接:
{
"_id" : ObjectId("50fd8bd460e958aa38000002"),
"created_at" : 1358793684,
"updated_at" : 1358793684,
"is_active" : 0,
"memberships" : [
{
}
],
"first_models" : [
[ ]
],
"second_models" : [
[ ]
],
"pages" : [
[ ]
]
}
我想要的是更新一些文档它的给定_id。特别是,我需要在"first_models"数组。从我的Codeigniter模型中,我称之为:
$result = $this->mongo_db
->push(array('_id' => '50fd8bd460e958aa38000002'),array('first_models' => array('Some text here')))
->update($this->collections['sites']);
当我尝试更新一个特定的文档时,我得到这个错误:
Mod on _id not allowed
似乎有一些冲突与给定的_id…我错过了什么,我不知道是什么我使用Mongodb 2.2.2与Codeigniter 2.1.3, PHP5.4和Apache2。
我走错方向了。@Rajan终于给我指出了正确的解决方案:
$updated = $this->mongo_db->where('_id', new MongoId($id))
->set($data)
->update('mycollection');
您可以看到,使用set
代替push
,并使用new MongoId($id)
引用对象id:-)