MongoDB provides a powerful set of commands that allow us to interact with the database, view collections, monitor MongoDB, and perform administrative tasks from CLI or GUI. Here, we provided some common commands used in MongoDB. Note that all these commands are executed with MongoDB 6.0 version.
1. Most Useful Commands of MongoDB
The legacy mongo shell command mongo was deprecated in MongoDB 5.0 and removed in MongoDB 6.0. The new mongo shell mongosh has improved and has many advantages, for example, compatibility with the MongoDB Node.js driver.
1.1 MongoDB –nodb command
The MongoDB Shell can be launched without first connecting to a particular database with the mongosh –nodb command. As a result, we can perform administrative commands, connect to databases, and complete other operations using the shell without a special database environment.
# Usage of –nodb command
mongosh –nodb
Hence, we are in the mongosh shell with no database connection.
1.2 show.help command
In MongoDB, information on databases, collections, indexes, and other system components can be viewed using the show command. To see a list of available sub-commands for the show command, we can use the help option like this.
# Usage of show.help command
show.help
Therefore, it displayed a list of the subcommands that are accessible to use with the show command.
1.3 show dbs command
Moreover, the show command can be used to view the database currently present in MongoDB of our system. We can use the show command with the databases keyword or either dbs. Both generate the same results.
# Usage of show dbs command
show dbs
This, the above command generates a list of available databases below.
1.4 MongoDB show “collections” command
Similarly, the collections can also be viewed via the show command of MongoDB. All we have to do is placed the keyword collections along with the show command performed below.
# Usage of show collections command
show collections
The collections stored in the database are listed as follows.
1.4 “setFreeMonitoring” command
Next, we have the setFreeMonitoring command of MongoDB which is available in MongoDB version 4.0 and later, and it is used to set up a free tier monitoring service. The setFreeMonitoring command called with the action parameter is used to enable or disable free cloud-based monitoring for a MongoDB deployment. Here’s a command of setFreeMonitoring with the enable action.
# Usage of setFreeMonitoring command
db.adminCommand(
{
setFreeMonitoring: 1,
action: “enable”
}
)
Similarly, we used the disable action to disable the free monitoring service like this.
# Usage of disable option
db.adminCommand(
{
setFreeMonitoring: 1,
action: “disable”
}
)
1.5 “CreateUser” command
Further, if we are required to create a new user with a specified set of privileges, then, we should use the CreateUser command of MongoDB is given as follows.
# Usage of CreateUser command
db.adminCommand(
{
createUser: “User21”,
pwd: passwordPrompt(),
roles: [
{ role: “dbOwner”, db: “admin” }
]
}
)
Above, we create a new user named User21 with the password entered by the user at runtime. The new user is granted the dbOwner role on the admin database, which provides full access to the database.
Hence, the output verifies that the user is created under the specified privileges.
1.6 “hostInfo” command
There, we have the hostInfo command which is the MongoDB administrative command. It retrieves information about the host where the MongoDB instance is running. Here is the command given with its parameters.
# Usage of hostInfo command
db.adminCommand(
{
hostInfo: 1
}
)
The output displayed various details about the host, including its operating system, architecture, and CPU information.
1.7 “explain” command
There, we have another command which is explain to provide information about a query execution by the database. The explain command here is used with the find command.
# Usage of explain command
db.student.find().explain()
Here, the explain() command is used to get an object that provides information about the query plan used by MongoDB to execute the find() method on the student collection.
The following output contains the details about the query.
More details about explain command can be found here.
1.8 MongoDB “getLogComponents” command
In addition to all the above commands, we have the getLogComponents command in MongoDB to retrieve information about the current logging verbosity and levels for various components of the MongoDB system.
# Usage of getLogComponents command
db.getLogComponents()
Here, it includes the following logging information for all components of the MongoDB system.
More details about other MongoDB commands can be found here.
2. Conclusion
In conclusion, we have discussed some common commands of MongoDB. Among the many commands that MongoDB supports, these are just a few. However, the MongoDB documentation has an extensive set of commands.
MongoDB provides a powerful set of commands that allow us to interact with the database, view collections, monitor MongoDB, and perform administrative tasks from CLI or GUI. Here, we provided some common commands used in MongoDB. Note that all these commands are executed with MongoDB 6.0 version. 1. Most Useful Commands of MongoDB The legacy Read More MongoDB