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.
Use db.admin().listDatabases
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With