Listing the databases in MongoDB

Viewed 13

Why is this code code not list the databases in MongoDB Atlas?

shane@XPS:~/demo$ node -v
v12.22.9
shane@XPS:~/demo$ npm list mongodb 
shane@ /home/shane
├── mongodb@4.10.0
└─┬ mongoose@6.5.2
  └── mongodb@4.8.1

The main code is in demo.js

const { MongoClient } = require('mongodb')

async function main(){
  const uri = "mongodb+srv://<username>:<password>@cluster0.asnad.mongodb.net/?retryWrites=true&w=majority";

const client = new MongoClient(uri);

try {
  await client.connect();

  await listDatabases(client);

} catch(e) {
    console.error(e);
  } finally {
    await client.close();
  }

}

main().catch(console.error);

async function listDatabases(client) {
  const databasesList = await client.db().admin().listDatabases();

console.log("Databases:");
databasesList.databases.forEach(db => {
  console.log('- ${db.name}');
})
}

The username and password are not in the connection string but are working in my version.

In the terminal I typed and got,

shane@XPS:~/demo$ node demo.js
Databases:
- ${db.name}
- ${db.name}
- ${db.name}

Why is it not listing the databases? I'm not getting the actual database names.

This came from the YouTube video https://www.youtube.com/watch?v=fbYExfeFsI0

0 Answers
Related