douguai6716 2012-04-04 09:36
浏览 14
已采纳

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

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" : [] }
  • 写回答

1条回答 默认 最新

  • doudun1934 2012-04-04 13:15
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?