PHP & MongoDB Examples
- PHP & MongoDB - Connect Database
- PHP & MongoDB - Show Databases
- PHP & MongoDB - Drop Database
- PHP & MongoDB - Create Collection
- PHP & MongoDB - Drop Collection
- PHP & MongoDB - Display Collections
- PHP & MongoDB - Insert Document
- PHP & MongoDB - Select Document
- PHP & MongoDB - Update Document
- PHP & MongoDB - Delete Document
- PHP & MongoDB - Embedded Documents
- PHP & MongoDB - Error Handling
- PHP & MongoDB - Limiting Records
- PHP & MongoDB - Sorting Records
PHP & MongoDB Useful Resources
PHP & MongoDB - Drop Database
Steps to Drop Database
First step to do any operation is to create a MongoDB Client instance.
$uri = "mongodb://localhost:27017"; // connect to mongodb $client = new MongoDB\Client($uri);
Second step is to prepare and execute a command to drop a database.
// select a database
$db = $client->myDb;
// execute a command
$cursor=$db->command(array('dropDatabase' => 1));
Example - Dropping a MongoDB Databases
Try the following example to drop a database from a MongoDB server −
index.php
<?php
require __DIR__ . '\vendor\autoload.php';
$uri = "mongodb://localhost:27017";
// connect to mongodb
$client = new MongoDB\Client($uri);
// select a database
$db = $client->myDb;
// execute a command
$cursor=$db->command(array('dropDatabase' => 1));
echo "Database dropped."
?>
Output
Access the index.php deployed on apache web server and verify the output.
Database dropped.
Advertisements