简体   繁体   中英

update a mongodb collection in php

I am trying to update a part of mongodb collection in php. So I passed two parameters $id and $val using html form and I would like to update the value of a document with id=$id to val=$val. this is what I did so far in my php code, but I couldn't still get the document updated. your help is needed

$m = new MongoClient();
$db=$connection->mydb;
$user_collection=$db->mycollection;
$user_collection->update({"_id": ObjectId($id)},{$set: "value":$val}});

Thanks in advance

{"_id": ObjectId($id)},{$set: "value":$val}}绝不是有效的PHP代码,请查看如何编写数组

据我所知,正确的语法是 -

$user_collection->updateOne(['_id' => new \MongoDB\BSON\ObjectID($id)], ['$set' => ['value' => $val]]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM