The most important MongoDB commands in an overview

Finding the MongoDB commands you need will make your work with the MongoDB Database Management System easier and more effective. In the following article, we outline the most important commands in this database management system.

What you should know about MongoDB before beginning

The NoSQL database MongoDB has become a leading alternative to MySQL. Users appreciate the high scalability and flexibility available in its database management system. If you’re thinking about trying out this software for the first time, it may be helpful to first take a look at our comprehensive MongoDB tutorial. But if you are already familiar with MongoDB, the following overview of the most important MongoDB commands will help make your workday significantly easier.

What is the syntax of the commands?

Before we get into the different commands, it is important to first understand the syntax of MongoDB commands. The MongoDB commands always follow the same patterns, and these are formed as follows:

db.runCommand ( { hello: 1 } )

What are the most important MongoDB commands when getting started?

The basic MongoDB commands will help you get started and ensure that you can work effectively with the software. Here are the most important MongoDB commands when you are starting out:

MongoDB commands Description
db.help() This command lists all available MongoDB commands.
mongo -version Use this command to determine which version of MongoDB you are currently using. Execute the command in the Linux or macOS terminal. Use the CMD prompt if you are using Windows. You should see the shell version you are currently using and the corresponding MongoDB server.

What are the commands for databases?

You need databases to store your data. The following MongoDB commands are particularly important for this:

MongoDB commands Description
show dbs You can display all databases as a list with this command.
use DATABASE_NAME Create a new database and choose the name with this command.
db Use this command to query which database you have selected.
db.dropDatabase() Use this command to delete the selected database.

What are the commands for collections?

While relational databases like MySQL rely on tables, MongoDB uses collections. The following MongoDB commands are for collections:

MongoDB commands Description
db.createCollection (Name, Options) Creates a simple collection and specifies its name and other options if needed. The collection can also be limited.
show collections Displays and lists all available collections.
collectionName.drop() Use this command to delete a collection. If the collection was successfully deleted, the system will confirm this with “true”. You will be shown "false" if there is an error.

What are the commands for user management?

User profiles must be created and managed to allow different users to work on a database. The following MongoDB commands and some others can help you do this:

MongoDB commands Description
createUser (user, writeConcern) Creates a new user. "writeConcern" sets a permission level.
dropUser Deletes a single user from the database.
dropAllUsersFromDatabase Deletes all users who are stored in a database.
usersInfo Shows all available user information.
updateUser Updates the user data.
grantRolesToUser Gives a user certain rights or roles.
revokeRolesFromUser Removes certain rights or roles from a user.

What are the commands for roles?

You can assign specific rights or roles to users. The following MongoDB commands are required to manage, specify or delete rights or roles:

MongoDB commands Description
createRole Creates a role and defines its rights and duties.
rolesInfo Queries the specifications of a role.
updateRole Updates a role and the existing information.
dropRole Deletes a specific role.
dropAllRolesFromDatabase Deletes all roles in a database.
grantPrivilegesToRole Adds clearly defined privileges to a role.
revokePrivilegesFromRole Removes individual privileges from a role.
grantRolesToRole Defines roles whose privileges are passed on to another role.
revokeRolesFromRole Removes inherited roles.
invalidateUserCache Clears the user cache and removes information about roles.

How can I add and manage documents?

The following MongoDB commands are useful for modifying documents and managing them in collections:

MongoDB commands Description
insert Adds a document or documents to a collection.
update Updates one or more documents.
delete Deletes documents from a collection.
find Selects and displays specific documents from a collection.
findAndModify Displays and modifies a specific document.
getMore Outputs documents which are selected with the cursor.
getLastError Displays the status of the last operation performed.

How can I group and sort?

The database offers aggregation commands to sort documents efficiently. You can perform grouping with these commands:

MongoDB commands Description
aggregate Groups documents.
count Counts the different documents in a collection.
distinct Displays defined values and determines how often they occur in a collection.
mapReduce Used for large data sets and sorts them.

Which MongoDB commands are relevant for security purposes?

MongoDB is also appropriate for working with sensitive data. You can place restrictions within the system and protect records by requiring authentication. You should know the following commands to increase security in MongoDB:

MongoDB commands Description
authenticate Starts an authenticated session that requires a username and password.
getnonce Use this command to generate a unique password for a protected login.
logout Ends the current protected session.

What are commands for sessions?

MongoDB first created commands for specific sessions in versions 3.6 and 4. These commands may be of interest to you when you are working:

MongoDB commands Description
startSession Starts a new session.
refreshSessions Updates inactive sessions.
endSessions Ends sessions before the estimated time.
killSessions Stops specified sessions.
killAllSessions Stops all sessions immediately.
killAllSessionsByPattern Stops all sessions that match certain defined parameters.
commitTransaction Performs a transaction.
abortTransaction Cancels a transaction.

What are other administrative commands?

MongoDB has other commands to make administrative work easier. Some of the most important ones are listed in the following table in alphabetical order:

MongoDB commands Description
cloneCollectionAsCapped Copies an uncapped collection as a new capped collection.
collMod Adds options to a collection.
compact Defragments a collection and redesigns the indexes.
convertToCapped Converts a collection without a cap to a collection with a cap.
createIndexes Adds one or more indexes to a collection.
getParameter Displays configuration options.
listIndexes Lists all available indexes in a collection.
setParameter Modifies configuration options.
shutdown Interrupts the mongod or mongos process.

Diagnosis and monitoring

MongoDB also provides commands for monitoring and diagnostics. The following commands are helpful for carrying out these processes:

MongoDB commands Description
dbStats Provides statistics on the type and load of a particular database.
features Lists all available features.
serverStatus Returns the status of the server in use.
buildInfo Shows all available information about the current build of MongoDB.
connectionStatus Provides information about the current connection.
dataSize Shows the size of a file or a selected set of different files.
setFreeMonitoring Use this command to allow or disallow free monitoring during runtime.
Was this article helpful?
Page top