gpt4 book ai didi

php - MongoWriteConcernException。 (不可变)字段 '_id' 被发现已更改为 _id

转载 作者:IT老高 更新时间:2023-10-28 13:22:05 26 4
gpt4 key购买 nike

在 CodeIgniter 的 MongoDB 中进行更新时,出现以下错误:

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

这是我的 Controller 代码

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());
}
}

型号代码是

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;
}

最佳答案

您无法更新 _id 字段。

请注意,您的 $userdata 对象变量包含 _id 字段,然后您继续将该 $userdata 对象作为要传递的值更新。因此,您正在尝试更新 _id 字段。

您需要在执行'$set'=>$userdata时从$userdata中删除_id

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

关于php - MongoWriteConcernException。 (不可变)字段 '_id' 被发现已更改为 _id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32457569/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com