使用PHP从mongo集合中删除文档


Removing documents from a mongo collection with PHP

我正在建立一个基于web的库存系统,并试图实现在一个项目输入错误的情况下删除功能。我看过的所有例子看起来都很简单,但我似乎无法让它工作。

<?php
   // Start the session
   session_start();
   $id = $_SESSION['id'];
   $mongo = new MongoClient(); // connect
   $db = $mongo->test; // set db
   $collection = $db->check_out; // set collection
   $collection->remove(array('_id' => new MongoId($id)), true);
   header('Location: main.php');
?>

我已经打印出$id,并看到它与存储在集合中的值匹配。

尝试使用

$collection->remove(array('_id' => new MongoId($id)), array("justOne" => true));

justOne -表示需要删除的元素个数