MongoDb Update in PHP

278 views Asked by At

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.

1

There are 1 answers

0
Prabal Thakur On
$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