Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
42 lines (34 sloc) 1.25 KB
const {MongoClient} = require('mongodb');
async function main(){
* Connection URI. Update <username>, <password>, and <your-cluster-url> to reflect your cluster.
* See for more details
const uri = "mongodb+srv://<username>:<password>@<your-cluster-url>/test?retryWrites=true&w=majority";
* The Mongo Client you will use to interact with your database
* See for more details
const client = new MongoClient(uri);
try {
// Connect to the MongoDB cluster
await client.connect();
// Make the appropriate DB calls
await listDatabases(client);
} catch (e) {
} finally {
// Close the connection to the MongoDB cluster
await client.close();
* Print the names of all available databases
* @param {MongoClient} client A MongoClient that is connected to a cluster
async function listDatabases(client){
databasesList = await client.db().admin().listDatabases();
databasesList.databases.forEach(db => console.log(` - ${}`));
You can’t perform that action at this time.