MongoWriteConcernException. it was found that the (immutable) field "_id" was changed to _id

When upgrading to MongoDB in CodeIgniter, I have the following error:

Type: MongoWriteConcernException

Message: localhost:27017: After applying the update to the document {_id: ObjectId('55ee98543bd7af780b000029') , ...}, the (immutable) field '_id' was found to have been altered to _id: "55ee98543bd7af780b000029"

Filename: C:\xampp\htdocs\CI\application\models\mongo_model.php

Here is my controller code

public function  update()
{
    $userdata['firstname'] = $this->input->post('txtfirstname');
    $userdata['lastname'] = $this->input->post('txtlastname');
    $userdata['email'] =  $this->input->post('txtemail');
    $userdata['password'] = md5($this->input->post('txtpassword'));
    $userdata['_id'] = $this->input->post('hiddenId');
    $collection=  $this->mongo_model->updateuserdb($userdata);
    if ($collection)
    {
        header('location:'.base_url()."index.php/user".$this->index());
    }
}

and model code

public function updateuserdb($userdata)
{
    $id = $userdata['_id'];
    $collection = $this->mongo_db->db->selectCollection('myfirstCollection');
    $query = $collection->update(array('_id' => new MongoId($id)), array('$set' => $userdata), array('upsert' => FALSE));
    return $query;
}
+4
source share
1 answer

You cannot update the field _id.

Note that your object variable $userdatacontains a field _id, and then you pass that object $userdataas the value to be updated. As a result, you are trying to update the field _id.

You need to remove _idfrom $userdatawhen executing '$ set' => $ userdata strong>.

$collection->update(array('_id'=>new MongoId($id)),array('$set'=>$userdata),array('upsert'=>FALSE));
+3

Source: https://habr.com/ru/post/1606403/


All Articles