I am able to dropDatabase using the Node JS Driver Db class Method dropDatabase.
But the problem is, I have to connect to the Database before dropping it as follows:
var MongoClient = require('mongodb').MongoClient;
var databaseName = 'myMongoDatabaseName'
var connStr = 'mongodb://localhost:27017/' + databaseName;
MongoClient.connect(connStr, function(err, db) {
// Let's drop the database
db.dropDatabase(function(err, result) {
console.dir('we dropped the database ');
});
db.close();
});
I am able to list all the databases in the MongoDb, just connecting to the root as follows:
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/', function(err, db) {
db.admin().listDatabases().then(function(dbs) {
// Grab the databases
dbs = dbs.databases;
for(var i = 0; i < dbs.length; i++) {
console.dir(dbs[i].name);
}
db.close();
});
});
My question is, if I have 10 databases, then every time I have to connect to database and drop it (repeat for 10 databases)?
Is their any way like, connect to root (without specifying any database Name) and drop the database using the specific Database Name?