how to list all collections from a database in mongodb node js

Solutions on MaxInterview for how to list all collections from a database in mongodb node js by the best coders in the world

showing results for - "how to list all collections from a database in mongodb node js"
Micaela
19 Jan 2018
1var mongo = require('mongodb').MongoClient;
2
3async function connect(){
4    /**
5     * Connection URI. Update <username>, <password>, and <your-cluster-url> to reflect your cluster.
6     * See https://docs.mongodb.com/ecosystem/drivers/node/ for more details
7     */
8    const uri = "yourUri";
9 
10
11    const client = new mongo(uri);
12 
13    try {
14        // Connect to the MongoDB cluster
15        await client.connect();
16        
17        // Make the appropriate DB calls
18        const db = client.db("testDatabase");
19       
20        const collections = await db.collections();
21        collections.forEach (c=>console.log(c.collectionName));
22        
23 
24       
25       
26 
27    } catch (e) {
28        console.error(e);
29    } finally {
30        await client.close();
31    }
32}
33
34connect().catch(console.error);
35