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

What you should know about MongoDB before beginning

The NoSQL database MongoDB has become a leading al­ter­na­tive to MySQL. Users ap­pre­ci­ate the high scal­a­bil­i­ty and flex­i­bil­i­ty available in its database man­age­ment 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 com­pre­hen­sive MongoDB tutorial. But if you are already familiar with MongoDB, the following overview of the most important MongoDB commands will help make your workday sig­nif­i­cant­ly easier.

What is the syntax of the commands?

Before we get into the different commands, it is important to first un­der­stand 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 ef­fec­tive­ly with the software. Here are the most important MongoDB commands when you are starting out:

MongoDB commands De­scrip­tion
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 cor­re­spond­ing MongoDB server.

What are the commands for databases?

You need databases to store your data. The following MongoDB commands are par­tic­u­lar­ly important for this:

MongoDB commands De­scrip­tion
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.drop­Data­base() Use this command to delete the selected database.

What are the commands for col­lec­tions?

While re­la­tion­al databases like MySQL rely on tables, MongoDB uses col­lec­tions. The following MongoDB commands are for col­lec­tions:

MongoDB commands De­scrip­tion
db.cre­ate­Col­lec­tion (Name, Options) Creates a simple col­lec­tion and specifies its name and other options if needed. The col­lec­tion can also be limited.
show col­lec­tions Displays and lists all available col­lec­tions.
col­lec­tion­Name.drop() Use this command to delete a col­lec­tion. If the col­lec­tion was suc­cess­ful­ly deleted, the system will confirm this with “true”. You will be shown "false" if there is an error.

What are the commands for user man­age­ment?

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 De­scrip­tion
cre­a­teUser (user, writeCon­cern) Creates a new user. "writeCon­cern" sets a per­mis­sion level.
dropUser Deletes a single user from the database.
dropAl­lUsers­From­Data­base Deletes all users who are stored in a database.
usersInfo Shows all available user in­for­ma­tion.
up­da­teUser Updates the user data.
grantRolesToUser Gives a user certain rights or roles.
re­vokeRoles­Fro­mUser 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 De­scrip­tion
cre­ate­Role Creates a role and defines its rights and duties.
rolesInfo Queries the spec­i­fi­ca­tions of a role.
up­date­Role Updates a role and the existing in­for­ma­tion.
dropRole Deletes a specific role.
dropAll­Roles­From­Data­base Deletes all roles in a database.
grant­Priv­i­legesTo­Role Adds clearly defined priv­i­leges to a role.
re­vokePriv­i­leges­From­Role Removes in­di­vid­ual priv­i­leges from a role.
grantRolesTo­Role Defines roles whose priv­i­leges are passed on to another role.
re­vokeRoles­From­Role Removes inherited roles.
in­val­i­da­teUser­Cache Clears the user cache and removes in­for­ma­tion about roles.

How can I add and manage documents?

The following MongoDB commands are useful for modifying documents and managing them in col­lec­tions:

MongoDB commands De­scrip­tion
insert Adds a document or documents to a col­lec­tion.
update Updates one or more documents.
delete Deletes documents from a col­lec­tion.
find Selects and displays specific documents from a col­lec­tion.
fin­d­And­Mod­i­fy Displays and modifies a specific document.
getMore Outputs documents which are selected with the cursor.
get­LastEr­ror Displays the status of the last operation performed.

How can I group and sort?

The database offers ag­gre­ga­tion commands to sort documents ef­fi­cient­ly. You can perform grouping with these commands:

MongoDB commands De­scrip­tion
aggregate Groups documents.
count Counts the different documents in a col­lec­tion.
distinct Displays defined values and de­ter­mines how often they occur in a col­lec­tion.
mapReduce Used for large data sets and sorts them.

Which MongoDB commands are relevant for security purposes?

MongoDB is also ap­pro­pri­ate for working with sensitive data. You can place re­stric­tions within the system and protect records by requiring au­then­ti­ca­tion. You should know the following commands to increase security in MongoDB:

MongoDB commands De­scrip­tion
au­then­ti­cate Starts an au­then­ti­cat­ed 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 De­scrip­tion
start­Ses­sion Starts a new session.
re­fresh­Ses­sions Updates inactive sessions.
end­Ses­sions Ends sessions before the estimated time.
killSes­sions Stops specified sessions.
kil­lAllSes­sions Stops all sessions im­me­di­ate­ly.
kil­lAllSes­sions­By­Pat­tern Stops all sessions that match certain defined pa­ra­me­ters.
com­mit­Trans­ac­tion Performs a trans­ac­tion.
abort­Trans­ac­tion Cancels a trans­ac­tion.

What are other ad­min­is­tra­tive commands?

MongoDB has other commands to make ad­min­is­tra­tive work easier. Some of the most important ones are listed in the following table in al­pha­bet­i­cal order:

MongoDB commands De­scrip­tion
cloneCol­lec­tionAsCapped Copies an uncapped col­lec­tion as a new capped col­lec­tion.
collMod Adds options to a col­lec­tion.
compact De­frag­ments a col­lec­tion and redesigns the indexes.
con­vert­To­Capped Converts a col­lec­tion without a cap to a col­lec­tion with a cap.
cre­ateIn­dex­es Adds one or more indexes to a col­lec­tion.
get­Pa­ra­me­ter Displays con­fig­u­ra­tion options.
listIn­dex­es Lists all available indexes in a col­lec­tion.
set­Pa­ra­me­ter Modifies con­fig­u­ra­tion options.
shutdown In­ter­rupts the mongod or mongos process.

Diagnosis and mon­i­tor­ing

MongoDB also provides commands for mon­i­tor­ing and di­ag­nos­tics. The following commands are helpful for carrying out these processes:

MongoDB commands De­scrip­tion
dbStats Provides sta­tis­tics on the type and load of a par­tic­u­lar database.
features Lists all available features.
server­Sta­tus Returns the status of the server in use.
buildInfo Shows all available in­for­ma­tion about the current build of MongoDB.
con­nec­tion­Sta­tus Provides in­for­ma­tion about the current con­nec­tion.
dataSize Shows the size of a file or a selected set of different files.
set­FreeMon­i­tor­ing Use this command to allow or disallow free mon­i­tor­ing during runtime.
Go to Main Menu