Explore Your MongoDB Landscape: How to List All Databases

2024-07-04

Concepts:

  • Database: In MongoDB, a database is a container that holds collections (similar to tables in relational databases). Each collection stores documents (JSON-like structures) representing your data.
  • MongoDB: An open-source, NoSQL document database that provides flexible data storage with high performance.
  • MongoDB Shell: An interactive tool that allows you to connect to a MongoDB server, execute commands, and manage your databases and collections.
  • MongoDB Query: In MongoDB, queries are used to retrieve specific documents from collections based on certain criteria. However, in this case, we're not using a query to retrieve data, but rather a command to list all databases.

How to List All Databases:

  1. Connect to MongoDB Shell: Open your terminal or command prompt and type the following command, replacing <host> and <port> with the actual host address and port number of your MongoDB server:

    mongo <host>:<port>
    

    For example:

    mongo localhost:27017
    
  2. List Databases: Once connected, type the following command in the MongoDB shell:

    show dbs
    

    This command will display all databases available on the server, including the default database named "local."

Understanding the Output:

The show dbs command outputs a list of databases, each with its size (in kilobytes) and the storage engine used. Keep in mind that this list might include internal MongoDB databases that aren't directly used for storing your application data.

Additional Notes:

  • Filtering Databases: While show dbs is the simplest method, you can use the db.adminCommand helper with the listDatabases argument to achieve more fine-grained control. For instance, you can filter based on database names or only show the database size:

    db.adminCommand({ listDatabases: 1, nameOnly: true })
    
  • User Permissions: The databases displayed may depend on your user's permissions on the MongoDB server. If you only have access to specific databases, you might not see all databases listed.




Example Codes for Listing Databases in MongoDB Shell

Listing all databases using show dbs:

# Assuming you're already connected to the MongoDB Shell

show dbs

This will display a list of all databases on the server, including their size and storage engine.

Listing only database names using db.adminCommand:

db.adminCommand({ listDatabases: 1, nameOnly: true })

This code retrieves a list of all database names without the size information. This approach is more efficient if you only need the names.

Filtering databases by name (using regular expressions):

db.adminCommand({
  listDatabases: 1,
  filter: { name: /^myDatabase_/ } // Filter for names starting with "myDatabase_"
})



Using db.adminCommand with different options:

While the previous example showcased filtering by name, db.adminCommand offers more flexibility:

  • Filtering by size:
db.adminCommand({
  listDatabases: 1,
  filter: { size: { $gt: 1024 } } // Filter for databases larger than 1MB
})
  • Skipping specific databases:
db.adminCommand({
  listDatabases: 1,
  filter: { name: { $nin: ["local", "admin"] } } // Exclude "local" and "admin" databases
})

Scripting with JavaScript:

You could write a short JavaScript script that connects to the MongoDB server, executes show dbs, and processes the output further. This allows for custom filtering or data manipulation:

const { MongoClient } = require('mongodb');

const uri = "mongodb://localhost:27017"; // Replace with your connection URI

MongoClient.connect(uri, (err, client) => {
  if (err) {
    console.error(err);
    return;
  }

  const db = client.db();
  const databases = db.adminCommand({ listDatabases: 1 });

  // Process the list of databases here (filter, analyze, etc.)
  console.log(databases.databases);

  client.close();
});

This approach requires a basic understanding of JavaScript and the MongoDB driver library. While less common for quick checks, it offers more programmatic control.


database mongodb mongodb-query


Should You Store Images Directly in Your Database? Exploring the Pros and Cons

Storing the Image (Yea):This method involves converting the image file into a format that computers understand (binary data) and then storing that data directly in a special field in the database called a BLOB (Binary Large Object)...


Taming the World: Best Practices for Storing International Addresses in Databases

Database concepts:Relational database: This is the most common type of database used for storing addresses. It organizes data in tables with rows and columns...


Keeping Your Data Organized: MySQL ALTER TABLE for Column Resizing

Concepts:MySQL: A popular open-source relational database management system (RDBMS) used for storing and managing data in a structured way...


Troubleshooting MySQL Error: Lost Connection to Server During Query (Error Code 2013)

Error Context:MySQL: This error occurs when you're working with a MySQL database, a popular open-source relational database management system (RDBMS)...


Authentication Fundamentals: Securing Your PostgreSQL Data

DatabaseA database is a structured collection of data that allows for storage, retrieval, and management of information...


database mongodb query