While making update in 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 is
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;
}
You cannot update the _id
field.
Notice that your $userdata
object variable contains the _id
field and then you proceed to pass that $userdata
object as the value to be updated. As a result, you are attempting to update the _id
field.
You need to remove the _id
from $userdata
when doing '$set'=>$userdata.
$collection->update(array('_id'=>new MongoId($id)),array('$set'=>$userdata),array('upsert'=>FALSE));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With