2012-04-04 58 views
1

我将MongoDB中的“Article”保存为_id整数。 当我想在php中用_id删除文章时,什么都不会发生。 我使用的代码是:无法使用PHP驱动程序删除MongoDB中的文档

$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" : [] } 
+0

'INTVAL( 491)'与您的文档不匹配_id'字段 – Mattias 2012-04-04 09:41:37

+0

这是一个错字,我在发布到SO时忘了清理。我改为491来测试如果没有现场匹配会发生什么。 – 2012-04-04 10:09:01

+0

为什么你的'_id'字段不是Bson.ObjectId类型? – 2012-04-04 10:13:48

回答

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 
+0

谢谢,它的工作原理!但我真的明白为什么我的方式失败了! – 2012-04-04 14:43:38

相关问题