MongoDB remove()方法在PHP中未定义 [英] MongoDB remove() method is undefined with PHP
问题描述
我正在使用PHP7 + mongoDB.与作曲家一起安装的PHP驱动程序.
I'm using PHP7 + mongoDB. PHP driver installed with composer.
我有以下代码:
<?php
require 'vendor/autoload.php';
$m= new MongoDB\Client("mongodb://127.0.0.1/");
$db = $m->test_database;
$collection = $db->test_table;
$document = array( "first_name" => "Dude", "last_name" => "Dudly" );
$collection->insertOne($document);
$cursor = $collection->find();
foreach ($cursor as $document) {
echo $document["first_name"] . "\n";
}
$collection->remove();
?>
这将按预期打印出" Dude ". 但也有以下例外情况:
This prints out "Dude" as expected. But also the following exception:
在...中调用未定义的方法MongoDB \ Collection :: remove()...
并且不会删除集合中插入的数据.
And the inserted data in the collection is not removed.
你知道这是怎么回事吗?
Any idea what's wrong here?
谢谢!
推荐答案
From the docs, remove
operation takes query filter for removing documents from collection.
$collection->remove($document, array("justOne" => true));// With filter to remove single entry
$collection->remove(array());//Empty filter will remove all the entries from collection.
您可以使用最新的php驱动程序 http://php.net/manual/zh/set.mongodb.php
Btw you can use newest driver for php http://php.net/manual/en/set.mongodb.php
1 :更新:没有意识到OP正在使用针对MongoDB的composer.
Update: Didn't realize OP was using composer for MongoDB.
您必须使用 deleteOne
和带有最新驱动程序的 deleteMany
变体.
You've to use deleteOne
and deleteMany
variant with latest driver.
$deleteResult = $collection->deleteOne(["first_name" => "Dude"]);
这篇关于MongoDB remove()方法在PHP中未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!