MongoDB show databases

MongoDB show databases

MongoDB show databases

Listing databases in MongoDB helps you see all available databases on a server. This guide covers how to show databases using MongoDB Shell, Node.js (Mongoose), and MongoDB Compass.

1. Show Databases Using MongoDB Shell

A. Open MongoDB Shell

Start the MongoDB shell by running:

mongosh

B. List All Databases

Run the following command:

show dbs

Example Output:

admin 0.000GB config 0.000GB local 0.000GB blogDB 0.001GB shopDB 0.002GB

The admin, config, and local databases are system databases, while blogDB and shopDB are user-created databases.

2. Show Databases Using Node.js (Mongoose)

A. Install Mongoose (If Not Installed)

npm install mongoose

B. Create a Script to List Databases

Create listDatabases.js:

const mongoose = require('mongoose'); const { MongoClient } = require('mongodb'); const url = 'mongodb://127.0.0.1:27017'; async function listDatabases() { const client = new MongoClient(url); try { await client.connect(); const databases = await client.db().admin().listDatabases(); console.log("📂 Databases on this server:"); databases.databases.forEach(db => console.log(`- ${db.name}`)); } catch (error) { console.error("❌ Error listing databases:", error.message); } finally { await client.close(); } } listDatabases();

C. Run the Script

node listDatabases.js

Example Output:

📂 Databases on this server: - admin - config - local - blogDB - shopDB

3. Show Databases Using MongoDB Compass

If you prefer a graphical interface, follow these steps:

  1. Open MongoDB Compass and connect to your server.
  2. The left sidebar will display all available databases.
  3. Click on a database name to view its collections.

✅ The databases will be listed visually.

4. Summary

Listed databases using MongoDB Shell (show dbs).
Used Mongoose to fetch databases programmatically.
Displayed databases in MongoDB Compass GUI.

5. Next Steps

🔹 Filter databases based on usage or size.
🔹 Monitor database performance and storage usage.
🔹 Secure database access using authentication.

Would you like help with MongoDB authentication or database monitoring? 🚀

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close