2017-03-16 9 views
2

私はPHP7 + mongoDBを使用しています。コンポーザーと共にインストールされたPHPドライバー。PHPでMongoDB remove()メソッドが未定義です

私は、次のコードを持っている:

<?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(); 
?> 

予想通りこれは "デュード" を出力します。 しかし、また次の例外:

中...

未定義のメソッドのMongoDB \コレクションへの呼び出し::(削除)、コレクション内の挿入されたデータは削除されません。

何が間違っていますか?

ありがとうございます!

+1

あなたは[この](https://github.com/mongodb/mongo-php-library/blob/master/src/Collection.php)ライブラリを使用している場合、実際に[はい、 'Collection' 'remove'メソッドはありません。なぜそれはすべきだと思いますか?それは何をすべきか? – deceze

答えて

1

docsから、remove操作では、コレクションからドキュメントを削除するためのクエリフィルタが必要です。

$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/en/set.mongodb.php

1のための最新のドライバを使用することができます。http://php.net/manual/en/mongocollection.remove.php

更新:OPはMongoDBのために作曲を使用していた実現しませんでした。

deleteOnedeleteManyを最新のドライバで使用する必要があります。

$deleteResult = $collection->deleteOne(["first_name" => "Dude"]); 
+0

ありがとうございました!驚くばかりの答え、あなたは私の日を救う! – user79382

関連する問題