3

How to perform drop() or remove() and other functions by using MongoDB drivers for php 7?

I refered https://docs.mongodb.org/v3.0/reference/method/js-collection/

but, I am not getting much documentation for PHongo (MongoDB driver for PHP 7).

Thank you in advance.

2

1 Answer 1

6

You can delete a collection like this:

$manager = new \MongoDB\Driver\Manager("mongodb://" . $username . ":" . $password . "@{$host}:{$port}");
$manager->executeCommand('database', new \MongoDB\Driver\Command(["drop" => "collection"]));

or you could follow the instructions in this guide:

$db = (new MongoDB\Client)->demo;

$result = $db->dropCollection('users');
var_dump($result);
Sign up to request clarification or add additional context in comments.

3 Comments

Using MongoDB\Driver is wrong. Unless you are writing a driver yourself, no application code should be using that. The "this guide" link in the answer is in fact the only correct part.
@NeilLunn I was under the impression the Client is deprecated (see php.net/manual/en/class.mongoclient.php) and am thus currently working on a Manager based implementation. Can you shed some light on your comment?
@BartGloudemans You are under the wrong impression, and that's perfectly understandable since I have personally complained about this now for years. The thing you "think" is deprecated in the old mongo driver. The actual driver implementation to use ships in two parts, and this clearly confuses a lot of people. Ask the actual maintainer and he will keep pointing to php.net/manual/en/mongodb.overview.php which shows the "composer" installed part as the bit you need to be using. That's what the doc link above actually points to.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.