From b189995b36096d7f398a247c97100baf00f3cc3d Mon Sep 17 00:00:00 2001 From: rjss836 Date: Mon, 11 Sep 2023 18:51:50 +0530 Subject: [PATCH] added databaselist code --- .env | 4 +++- app.js | 12 +++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/.env b/.env index 63aa747..0ea3f3c 100644 --- a/.env +++ b/.env @@ -1,2 +1,4 @@ MONGO_URI="mongodb+srv://rjReactDev:Deadrjs%407@cluster0.as2wbdp.mongodb.net/?retryWrites=true&w=majority" -PORT=3033 \ No newline at end of file +PORT=3033 +# local db url should be replaced +# MONGO_URI='mongodb://localhost/nodeapi' \ No newline at end of file diff --git a/app.js b/app.js index 5ab4139..201ab41 100644 --- a/app.js +++ b/app.js @@ -18,6 +18,7 @@ const uri = "mongodb+srv://rjReactDev:90QcwhEs8LNtwy7L@cluster0.as2wbdp.mongodb. const client = new MongoClient(uri, { useUnifiedTopology: true, + useNewUrlParser: true, serverApi: { version: ServerApiVersion.v1, strict: true, @@ -29,8 +30,9 @@ async function run() { // Connect the client to the server (optional starting in v4.7) await client.connect(); // Send a ping to confirm a successful connection - // await client.db("admin").command({ ping: 1 }); + await client.db("admin").command({ ping: 1 }); console.log("Pinged your deployment. You successfully connected to MongoDB!"); + await listDatabases(client); } finally { // Ensures that the client will close when you finish/error await client.close(); @@ -38,6 +40,14 @@ async function run() { } run().catch(console.dir); +//get all database list +async function listDatabases(client) { + databasesList = await client.db().admin().listDatabases(); + + console.log("Databases:"); + databasesList.databases.forEach(db => console.log(` - ${db.name}`)); +}; + //const postRoutes =require('./routes/post') //using destructuring method