Mongodb Show Collection
1. Show Collections Using MongoDB Shell
A. Open MongoDB Shell
Start the MongoDB shell by running:
B. Select a Database
Choose the database you want to list collections for:
C. List All Collections
Run the following command:
✅ Example Output:
This displays all collections inside blogDB
.
2. Show Collections Using Node.js (Mongoose)
A. Install Mongoose (If Not Installed)
B. Create a Script to List Collections
Create listCollections.js
:
✅ Run the script:
✅ Example Output:
3. Show Collections Using Express.js API
A. Modify routes/dbRoutes.js
B. Test the API (Using cURL or Postman)
✅ Example Response:
4. Show Collections Using MongoDB Compass
If you prefer a graphical interface, use MongoDB Compass:
- Open MongoDB Compass and connect to your database.
- Click on the database name (e.g.,
blogDB
). - You will see a list of all collections in that database.
✅ The collections will be displayed on the screen.
5. Summary
✔ Listed collections using MongoDB Shell (show collections
).
✔ Used Mongoose to list collections via a script.
✔ Created an Express.js API to fetch collections dynamically.
✔ Used MongoDB Compass for a GUI-based approach.
6. Next Steps
🔹 Filter collections based on specific criteria.
🔹 Implement authentication to secure database queries.
🔹 Monitor database usage and optimize queries.
Would you like help with database security or query optimization? 🚀