【问题标题】:Mongodb document update error with Codeigniter: Mod on _id not allowed使用 Codeigniter 的 Mongodb 文档更新错误:Mod on _id not allowed
【发布时间】:2013-01-22 10:32:32
【问题描述】:

我正在尝试将 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有些冲突...我遗漏了一些东西,我不知道是什么:( 我正在使用带有 Codeigniter 2.1.3、PHP5.4 和 Apache2 的 Mongodb 2.2.2。

【问题讨论】:

    标签: php codeigniter mongodb codeigniter-2 mongodb-php


    【解决方案1】:

    我走错了方向。最后@Rajan 指出了正确的解决方案:

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

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

    【讨论】:

      猜你喜欢
      • 2014-07-29
      • 2013-04-19
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      • 2013-06-22
      相关资源
      最近更新 更多