2013-01-22 46 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) :-)