无法使用PHP驱动程序删除MongoDB中的文档

时间:2012-04-04 09:36:49

标签: php mongodb

我将MongoDB中的“Article”保存为整数_id。 当我想在php中删除带有_id的文章时,没有任何反应。 我使用的代码是:

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

无论使用和不使用“安全”选项,我都尝试了两种方法。当我回显$ result时,它是bool(true)。

非常感谢任何建议!

{ "_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" : [] }

1 个答案:

答案 0 :(得分:1)

你在集合名称中有拼写错误。

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

应该是:

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

安全选项不会确认删除了某些内容,只是没有错误。删除不会在删除不存在的内容时触发错误。

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

请注意,您不需要将整数重新整形为整数 - 如果您执行需要将输入转换为整数 - 请使用强制转换语句:

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