将php中的变量作为参数传递给mongodb remove()函数

时间:2015-09-13 19:27:59

标签: php mongodb

我想删除mongodb-php中的文档。我接受用户的id并使用我要删除的文件,但它给了我一个错误

  

“不推荐使用:MongoCollection :: remove():传递标量值   options参数已弃用,将在不久的将来删除   在C:\ wamp \ www..process.php第12行“。

这是我的代码

<?php  
$m = new mongo();
echo "Connection to database successfully";
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->mycollection;
echo "Collection selected succsessfully";
$collection->remove(array("Team_ID"=>$_POST['team_id']),false);
echo "Documents deleted successfully";
$cursor = $collection->find();
// iterate cursor to display team_id of documents
echo "Updated document";
foreach ($cursor as $document) {
echo $document["Team_ID"] . "\n";
}
?>

1 个答案:

答案 0 :(得分:1)

如错误所述,它不接受标量值作为第二个参数。相反,使用带有选项(http://php.net/manual/en/mongocollection.remove.php)的数组。

$collection->remove(array('Team_ID' => $_POST['team_id']), array('justOne' => false));

默认情况下,“justOne”为false,您可以省略第二个参数。

$collection->remove(array('Team_ID' => $_POST['team_id']));