2013-01-22 69 views
0

我试图用笨与codeigniter-mongodb-library/tree/v2,以便与蒙戈和更新文件与这种结构连接:MongoDB的文件更新错误:国防部在_id不允许

{ 
"_id" : ObjectId("50fd8bd460e958aa38000002"), 
"created_at" : 1358793684, 
"updated_at" : 1358793684, 
"is_active" : 0, 
"memberships" : [ 
    { 
    } 
], 
"first_models" : [ 
    [ ] 
], 
"second_models" : [ 
    [ ] 
], 
"pages" : [ 
    [ ] 
] 
} 

我想要的是一些更新文件由它给出_id。特别是我需要在“first_models”数组内添加元素。从我笨模型我打电话这样的:

$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。

回答

0

我正在走错了方向。 @Rajan指向我解决方案:

$updated = $this->mongo_db->where('_id', new MongoId($id)) 
->set($data) 
->update('mycollection'); 

你可以看到,使用set代替push,并引用对象ID与new MongoId($id) :-)