Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list all MongoDB databases in Node.js?

I've tried to find a solution to this question in: http://mongodb.github.io/node-mongodb-native/

However, I could not find a solution to listing all available MongoDB databases from a Node.js app.

like image 595
sprijk Avatar asked May 15 '13 17:05

sprijk


2 Answers

Use db.admin().listDatabases.

like image 88
paulmelnikow Avatar answered Oct 01 '22 00:10

paulmelnikow


You can do this now with the Node Mongo driver (tested with 3.5)

const MongoClient = require("mongodb").MongoClient;

const url = "mongodb://localhost:27017/";
const client = new MongoClient(url, { useUnifiedTopology: true }); // useUnifiedTopology removes a warning

// Connect
client
  .connect()
  .then(client =>
    client
      .db()
      .admin()
      .listDatabases() // Returns a promise that will resolve to the list of databases
  )
  .then(dbs => {
    console.log("Mongo databases", dbs);
  })
  .finally(() => client.close()); // Closing after getting the data
like image 40
John Alexis Guerra Gómez Avatar answered Sep 30 '22 22:09

John Alexis Guerra Gómez