Understanding MongoDB Collections
MongoDB is a document database, and data is organized within databases and collections. A database is a container for one or more collections, and a collection is a grouping of MongoDB documents. Understanding how to list these collections is a fundamental skill when working with MongoDB. This tutorial will cover several methods for listing collections within the MongoDB shell.
Connecting to a Database
Before listing collections, ensure you’re connected to the desired MongoDB instance and have selected the database you wish to inspect. You can connect using the mongo
shell command, providing the connection string if necessary.
To select a database, use the use
command followed by the database name:
use myDatabase
This command switches the current context to the myDatabase
database. Any subsequent operations will be performed within this database unless you switch to another one.
Listing Collections
MongoDB provides several ways to list collections within the currently selected database. Here are the most common methods:
1. show collections
Command:
This is the simplest and most direct way to list all collections in the current database. Just type the following in the MongoDB shell:
show collections
This will output a list of collection names, each on a new line.
2. show tables
Command:
The show tables
command is an alias for show collections
and achieves the same result. It’s provided for those familiar with relational database terminology.
show tables
3. db.getCollectionNames()
Method:
This JavaScript method provides a programmatic way to retrieve a list of collections. It returns an array of strings, where each string represents the name of a collection.
db.getCollectionNames()
This method is particularly useful when scripting or automating tasks. For example, you might iterate through the returned list to perform operations on each collection.
Example:
const collectionNames = db.getCollectionNames();
print("Collections in the current database:");
collectionNames.forEach(name => print(name));
System Collections
MongoDB automatically creates several system collections, typically prefixed with system.
. These collections store metadata about the database and its components, like indexes and access control information. They are included in the results of the above methods.
Listing All Databases
If you need to list all available databases on the MongoDB server, use the show dbs
command:
show dbs
This will display a list of all databases present on the server. Remember to select the desired database using the use
command before listing its collections.
Advanced Information with db.system.namespaces.find()
For more detailed information about collections, including their creation options (e.g., whether they are capped collections with a specific size), you can query the db.system.namespaces
collection:
db.system.namespaces.find()
This will return a list of documents, each containing information about a single collection or database. The name
field within each document represents the collection or database name.