show all records in collection mongodb code example
Example 1: get all data in collection mongodb
db.collection_name.find().pretty()
Example 2: 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);