1. db.adminCommand(cmd)
The db.adminCommand() method is used to execute administrative operations in MongoDB. It allows you to send administrative commands to the database.
Syntax
db.adminCommand({ command: <command> })
Example
db.adminCommand({ listDatabases: 1 })
Explanation:
The above command lists all the databases available in your MongoDB instance. When you run it, you will see an output like this:
Output:
{
"databases": [
{ "name": "admin", "sizeOnDisk": 4096 },
{ "name": "test", "sizeOnDisk": 8192 }
],
"totalSize": 12288,
"ok": 1
}
This is a useful command for checking the databases and their sizes.
2. db.aggregate()
The db.aggregate() command is used for performing advanced data processing and aggregation. It processes data in stages like filtering, grouping, and transforming.
Syntax:
db.collection.aggregate([ pipeline ], options)
Example:
db.orders.aggregate([
{ $group: { _id: "$customerId", totalAmount: { $sum: "$amount" } } }
])
Explanation:
In this example, we are grouping the orders collection by customerId and calculating the total amount of orders for each customer.
Output:
[
{ "_id": 1, "totalAmount": 250 },
{ "_id": 2, "totalAmount": 500 }
]
The aggregation pipeline is a powerful tool to manipulate and analyze data in MongoDB.
3. db.cloneDatabase("hostname")
The db.cloneDatabase() command is used to copy data from one MongoDB instance to another.
Syntax
db.cloneDatabase("hostname")
Example
db.cloneDatabase("192.168.1.100")
Explanation:
This command copies the database from the server with the IP address 192.168.1.100. Note that this command is deprecated in newer versions of MongoDB. Always refer to the official documentation for updates.
4. db.commandHelp(command)
The db.commandHelp() command provides a description of a specific database command.
Syntax
db.commandHelp("<command>")
Example
db.commandHelp("aggregate")
Explanation:
Running this command displays detailed help and syntax for the aggregate command. This is particularly useful when you’re unsure about a command’s options and usage.
5. db.createCollection(name, options)
The db.createCollection() command creates a new collection with optional configurations.
Syntax
db.createCollection("name", options)
Example
db.createCollection("students", { capped: true, size: 5000 })
Explanation:
Here, a new collection named students is created. The capped option limits the collection size to 5,000 bytes. This is helpful when you need to manage storage for specific collections.
Output:
{ "ok": 1 }
6. db.createView()
The db.createView() command creates a read-only view of data based on an aggregation pipeline.
Syntax
db.createView("viewName", "sourceCollection", [ pipeline ])
Example
db.createView("highScorers", "students", [
{ $match: { score: { $gt: 90 } } }
])
Explanation:
This command creates a view named highScorers that includes students with scores greater than 90. Views are helpful when you need to present data in a specific format without altering the original collection.
7. db.dropDatabase(<writeConcern>)
The db.dropDatabase() command deletes the current database, including all its collections and documents.
Syntax
db.dropDatabase({ writeConcern })
Example
db.dropDatabase()
Explanation:
When you run this command, the current database is removed permanently. Be cautious when using it.
Output:
{ "dropped": "test", "ok": 1 }
8. db.getLogComponents()
The db.getLogComponents() command retrieves the current logging settings for the database.
Syntax
db.getLogComponents()
Example
db.getLogComponents()
Explanation:
This command displays the logging levels for different components of MongoDB. The output might look like this:
Output:
{
"verbosity": 0,
"components": {
"accessControl": { "verbosity": -1 },
"command": { "verbosity": -1 },
...
}
}
This is useful for debugging and optimizing your database operations.
Frequently Asked Questions
What is the purpose of db.adminCommand()?
The db.adminCommand() is used for executing administrative commands, such as checking database statuses or managing system configurations.
How is db.aggregate() different from normal queries?
The db.aggregate() command allows for advanced data processing using multiple stages, like grouping and filtering, which is not possible with simple queries.
Can I undo a db.dropDatabase() command?
No, the db.dropDatabase() command is irreversible. Always double-check before running it.
Conclusion
In this article, we discussed some essential MongoDB commands, their syntax, and use cases. From administrative operations with db.adminCommand() to data aggregation with db.aggregate(), each command serves a specific purpose in database management. Practice these commands regularly to build a strong foundation in MongoDB. These commands are not only useful for managing data but also play a significant role in interview preparation and real-world applications.
You can also check out our other blogs on Code360.