简体   繁体   中英

MongoDb Update in PHP

I have one collection test_posts in which there are certain records. There is field called 'is_published' which has either 1 or 0 value. Based on which I am showing the content on end user side. I can show the content but for update there is some change in query as per MongoDB.

Here is sample code:
$newdata = array('$set' => array("is_published" => $_REQUEST['is_published'] ));
$c->update(array("id" => "1"), $newdata);

I write this code for updating the only particular record.Its similar to MySql query like:
UPDATE test_posts SET is_published = '" . $_REQUEST['is_published'] ."' WHERE id= '" . $_REQUEST['id'] ."'";

Is my MongoDb query same as of MySql ? Please suggest any changes if required.

Please suggest how to update the record for particular request data in MongoDB using MongoClient.

$conn = new MongoClient();
$db = $conn->selectDB("your database name");
$db->your collection name->update(array("_id" => new MongoID("here id mongodb id will come which was auto assign when you insert")), array('$set' => array("your field to update" => "content of update")));

hope this will help you and note the update query will act as insert query if update field is not found

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