Hu Qiang
Hu Qiang

Reputation: 1882

Cannot remove document in MongoDB using PHP driver

I am saving "Article" in MongoDB as bellow with _id of integers. When I want to delete the article with the _id in php, nothing happens. The code I use is:

$result = $db->arcitle->remove(
    array("_id" =>intVal(41)),
    array('safe' => true)
);

I have tried both with and without using "safe" option, neither works. When I echo $result, it is bool(true).

Any suggestion is much appreciated!

{ "_id" : 41,
  "when" : Date( 1333318420855 ),
  "publisher" : "5",
  "title" : "10 Steps To The Perfect Portfolio Website",
  "raw" : "",
  "preview" : "",
  "thumbnail" : "",
  "content" : [ 
    "{}" ],
  "tags" : null,
  "votes" : 0,
  "voters" : [],
  "comments" : [] }

Upvotes: 1

Views: 470

Answers (1)

AD7six
AD7six

Reputation: 66170

you've got a spelling mistake in the collection name.

$result = $db->arcitle->remove(

Should probably be:

$result = $db->article->remove(array("_id" => 41));

The safe option will not confirm something was deleted, only that there was no error. Remove will not trigger an error deleting something that doesn't exist.

> db.foo.remove({_id: "I don't exist"})
> db.getLastError()
null

Note that you don't need to recast an integer as an integer - and if you do need to cast input as an integer - use a cast statement:

$string = "42";
$int = (int) $string; // $int === 42

Upvotes: 1

Related Questions