4

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?

5
  • in the document, it says dropDatabase use to drop the current connected database, and there is no other cmd to drop a database. Then you will probably need to loop 10 databases, connect to each of them and drop it Commented Jan 4, 2016 at 4:07
  • Don't use code snippets for code which can't be run in the browser. Commented Jan 4, 2016 at 6:05
  • @royhowie , I am sorry, i accept Node JS code doesn’t run on the browser. I will try to post node code at other places. But, can you please advise me, how to add code snippets as Valijon edited my post. Commented Jan 5, 2016 at 0:08
  • 1
    @user3278897 copy and paste the code into the editor, then highlight it and press command (if on Mac) or control (windows) K, which will indent the code by four spaces per line. You can also manually indent the code with an extra four spaces per like before pasting into the editor. Commented Jan 5, 2016 at 0:35
  • @royhowie, I dont know i can add comment saying like, thank you. Your suggestion worked for my new question. It formatted perfectly. I voted up your comment. Commented Jan 5, 2016 at 1:58

1 Answer 1

2

You could use the Db() constructor to create the database instance using the name, connect to it and call the instance's dropDatabase() method, all this done within the loop. The concept can be described with something like the below implementation (warning: untested!):

var mongodb = require('mongodb'),
    MongoClient = mongodb.MongoClient,
    server = new mongodb.Server("127.0.0.1", 27017),
    url = 'mongodb://localhost:27017/';

var dropAllDatabases = function (callback) {
    MongoClient.connect(url, function(err, db) {
        if(err) callback(err);
        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);
                new mongodb.Db(dbs[i].name, server, {}).open(function (error, client) {
                    if(error) callback(error);
                    // drop the database
                    client.dropDatabase(function(err, result) { 
                        if(err) callback(err);
                        client.close(); 
                    });                     
                });
            }
            db.close();
        });
    });     
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.