How to clear a MongoDB database?

MongoDBDatabaseBig Data Analytics

To clear, use dropDatabase. Following is the syntax −

use yourDatabaseName;
db.dropDatabase();

To clear a MongoDB database, first show all the databases −

> show dbs

This will produce the following output −

MyDB    0.000GB
admin    0.000GB
config    0.000GB
local    0.000GB
onlinecustomertracker 0.000GB
test    0.006GB

Now, let us delete the database onlinecustomertracker −

> use onlinecustomertracker
switched to db onlinecustomertracker
> db.dropDatabase();
{ "dropped" : "onlinecustomertracker", "ok" : 1 }

Following is the query to show all databases after deleting a database above −

> show dbs

This will produce the following output −

MyDB    0.000GB
admin    0.000GB
config    0.000GB
local    0.000GB
test    0.006GB
raja
Published on 13-May-2020 09:13:37
Advertisements