Answers for "list all collections in mongodb node js"

1

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

var mongo = require('mongodb').MongoClient;

async function connect(){
    /**
     * Connection URI. Update <username>, <password>, and <your-cluster-url> to reflect your cluster.
     * See https://docs.mongodb.com/ecosystem/drivers/node/ for more details
     */
    const uri = "yourUri";
 

    const client = new mongo(uri);
 
    try {
        // Connect to the MongoDB cluster
        await client.connect();
        
        // Make the appropriate DB calls
        const db = client.db("testDatabase");
       
        const collections = await db.collections();
        collections.forEach (c=>console.log(c.collectionName));
        
 
       
       
 
    } catch (e) {
        console.error(e);
    } finally {
        await client.close();
    }
}

connect().catch(console.error);
Posted by: Guest on March-19-2021
0

list all collections in the MongoDB shell

JavaScript (shell):
db.getCollectionNames()

Node.js:
db.listCollections()
Posted by: Guest on July-19-2020
0

show all collections in mongodb node js

async function connect(){
    /**
     * Connection URI. Update <username>, <password>, and <your-cluster-url> to reflect your cluster.
     * See https://docs.mongodb.com/ecosystem/drivers/node/ for more details
     */
    const uri = "yourUri";
 

    const client = new mongo(uri);
 
    try {
        // Connect to the MongoDB cluster
        await client.connect();
        
        // Make the appropriate DB calls
        const db = client.db("testDatabase");
       
        const collections = await db.collections();
        collections.forEach (c=>console.log(c.collectionName));

        }
        
        
 
        // Make the appropriate DB calls
       
 
    } catch (e) {
        console.error(e);
    } finally {
        await client.close();
    }
}

connect().catch(console.error);
Posted by: Guest on March-19-2021

Code answers related to "list all collections in mongodb node js"

Code answers related to "Javascript"

Browse Popular Code Answers by Language