4

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.

Pranav
  • 143
  • 1
  • 3
  • 15
  • 1
    Mongodb's primary key field is `_id`, not `id`. Make the query work in the `mongo` console, then translate it to PHP. – mnemosyn Jan 30 '14 at 14:39

1 Answers1

2
$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

Prabal Thakur
  • 129
  • 11