Skip to content

Commit b189995

Browse files
author
rjss836
committed
added databaselist code
1 parent 3d35317 commit b189995

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

.env

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
MONGO_URI="mongodb+srv://rjReactDev:Deadrjs%407@cluster0.as2wbdp.mongodb.net/?retryWrites=true&w=majority"
2-
PORT=3033
2+
PORT=3033
3+
# local db url should be replaced
4+
# MONGO_URI='mongodb://localhost/nodeapi'

app.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const uri = "mongodb+srv://rjReactDev:90QcwhEs8LNtwy7L@cluster0.as2wbdp.mongodb.
1818

1919
const client = new MongoClient(uri, {
2020
useUnifiedTopology: true,
21+
useNewUrlParser: true,
2122
serverApi: {
2223
version: ServerApiVersion.v1,
2324
strict: true,
@@ -29,15 +30,24 @@ async function run() {
2930
// Connect the client to the server (optional starting in v4.7)
3031
await client.connect();
3132
// Send a ping to confirm a successful connection
32-
// await client.db("admin").command({ ping: 1 });
33+
await client.db("admin").command({ ping: 1 });
3334
console.log("Pinged your deployment. You successfully connected to MongoDB!");
35+
await listDatabases(client);
3436
} finally {
3537
// Ensures that the client will close when you finish/error
3638
await client.close();
3739
}
3840
}
3941
run().catch(console.dir);
4042

43+
//get all database list
44+
async function listDatabases(client) {
45+
databasesList = await client.db().admin().listDatabases();
46+
47+
console.log("Databases:");
48+
databasesList.databases.forEach(db => console.log(` - ${db.name}`));
49+
};
50+
4151
//const postRoutes =require('./routes/post')
4252

4353
//using destructuring method

0 commit comments

Comments
 (0)