How to list all databases in atlas using nodejs driver?

I check to see if the db name is in atlas I update it but if the db is not I create the db . My code is something like :

`async function isDbExist(dbName) {

let {MongoClient} = require('mongodb') ;

let client = await new MongoClient(urlToAtlasCluster)

  if(! (await client.db(dbName).listCollections.toArray().length)) {

// db is not in atlas … createDb()
I know that I could have used the cursor returned by ‘listCollections’ to enhance performance .
But is there any way I can listdatabases in node application without using this lame workaround !!

Hey @Ali_M_Almansouri,

You can use the listDatabases command for just this purpose, then call the command from your Node.js code.

For example:

let {MongoClient} = require('mongodb');
const urlToAtlasCluster = 'mongodb+srv://....';
const client = new MongoClient(urlToAtlasCluster);

function containsEntry(array, value) {
  return array.filter(e => == value).length > 0;

async function run() {
  try {
    await client.connect();
    const admin = client.db("admin");
    const result = await admin.command({ listDatabases: 1, nameOnly: true });
    console.log(`Contains "myTestDatabase": ${containsEntry(result.databases, 'myTestDatabase')}`);
    console.log(`Contains "local": ${containsEntry(result.databases, 'local')}`);
  } finally {
    await client.close();

The listDatabases command optionally has a filter command field you could use to narrow down the results further if you so choose.


alexbevi , thank you for your reply . This answers my question, and the quality of the code you provided is mind blowing . I think I need to learn more about these commands that are callable on any db like :
client.(‘dbName’).command({dbStats:1}) ;
and others .
Thank you again :kissing_heart:

Hey @Ali_M_Almansouri, I’m glad this was helpful.

Just as an added note the driver has an Admin class that provides convenience APIs to some of these methods.

For example:

import { MongoClient } from 'mongodb';

const client = new MongoClient('mongodb://localhost:27017');
const admin = client.db().admin();
const dbInfo = await admin.listDatabases();
for (const db of dbInfo.databases) {
1 Like

Thank you for taking the time to add to your reply , I think I will be fine with this way :

Because it is consistent with how you would work with other databases that has your actual data .
I’m , however, thankful for you and ahighly appreciate your help . This community might be the best community I have ever seen ! quick relpy , percise info and even high quality code !
If you can link to a documentation where I can find all the classes of nodejs mongodb driver and the related methods I will appreciate it . What I am looking for is a documentation that takes the style of nodejs documentation style , like :

  • Class : MongoClient
    • MongoClient.db (dbName):
      • dbName : String
      • returns : object to manipulate database …
        I am not aware of any documentations like this !
        Thank you again !

The MongoDB Node.js Driver API documentation is likely what you’re looking for :slight_smile:

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.