while making updat in mongodb in codeigniter I have the following error plz help me to solve this issue, 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;
}
any help is appreciable, thanks in advance
Answer:
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));
0 nhận xét:
Đăng nhận xét