Untitled

 avatar
unknown
plain_text
a month ago
845 B
9
Indexable
import { MongoClient } from "mongodb"


async function processDb() {
    const url = "mongodb://127.0.0.1:27017";
    const client = new MongoClient(url);

    try {
        await client.connect();
        console.log("Succesfully connected to MongoDB!");
        const dbList = await client.db().admin().listDatabases();
        console.log(dbList);
        if (dbList.databases.length === 0) {
            console.log("No databases found!");
        } else {
            console.log("Databases:");  
            dbList.databases.forEach(db => {
                console.log(` - ${db.name}`);
            });
        }
    } catch (err) {
        console.log(err);
    } finally {
        if (client) {
            await client.close();
            console.log("Connection closed.");
        }

    }
}
processDb();
Editor is loading...
Leave a Comment