To list the databases in a system, you need to use the listDatabases()
method on an Admin
object. This means you need to create an instance of an Admin
object first. The list of databases is returned as the second parameter to the callback function and is a simple array of database objects.
The following code shows an example of creating an Admin
object and then using it to get a list of the databases on the MongoDB server:
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://localhost/admin", function(err, db) {
var adminDB = db.admin();
adminDB.listDatabases(function(err, databases){
console.log("Before Add Database List: ");
console.log(databases);
});
});