Gdcrocx Gdcrocx - 1 year ago 115
Node.js Question

How to get list of databases in MongoDB using NodeJs?

I have seen answers on C# and Java but not able to find anything on NodeJs. I have tried using cmd shell in Windows to get the required output but no luck.

I am aware that the same information can be taken in Mongo shell but the requirement is to get a list within the NodeJs app.

cmd = child_process.exec('"C:\\Program Files\\MongoDB\\Server\\3.2\\bin\\mongo.exe" admin ; db.getMongo().getDBNames()');

and also

var mongoServer = require('mongodb-core').Server;

var server = new mongoServer({
host: 'localhost'
, port: 27017
, reconnect: true
, reconnectInterval: 50 });

server.on('connect', function (_server) {


var cmdres = _server.command('db.adminCommand({listDatabases: 1})');

console.log("Result: " + cmdres);


Answer Source

You can use mongodb driver to get dbs as following

var MongoClient = require('mongodb').MongoClient;

// Connection url
var url = 'mongodb://localhost:27017/test';
// Connect using MongoClient
MongoClient.connect(url, function(err, db) {
  // Use the admin database for the operation
  var adminDb = db.admin();
  // List all the available databases
  adminDb.listDatabases(function(err, result) {


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download