Node & MongoDB - Drop Database
To drop a database, you can use database.drop() method to drop the selected database.
MongoClient.connect(url, function(error, client) {
// Connect to the database
const database = client.db('myDb');
// Drop the database
database.dropDatabase();
});
Example
Try the following example to drop a mongodb database −
Copy and paste the following example as mongodb_example.js −
const MongoClient = require('mongodb').MongoClient;
// Prepare URL
const url = "mongodb://localhost:27017/";
// make a connection to the database
MongoClient.connect(url, function(error, client) {
if (error) throw error;
console.log("Connected!");
// Connect to the database
const database = client.db('myDb');
// Drop the database
database.dropDatabase();
console.log("Database dropped!");
// close the connection
client.close();
});
Output
Execute the mysql_example.js script using node and verify the output.
node mongodb_example.js Connected! Database dropped!
Advertisements