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­tern­at­ive to MySQL. Users ap­pre­ci­ate the high scalab­il­ity and flex­ib­il­ity 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­hens­ive 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­ni­fic­antly 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­fect­ively 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­res­pond­ing MongoDB server.

What are the commands for databases?

You need databases to store your data. The following MongoDB commands are par­tic­u­larly 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­fully 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­ateUser (user, write­Con­cern) Creates a new user. "write­Con­cern" sets a per­mis­sion level.
dropUser Deletes a single user from the database.
dro­pAl­lUsersFrom­Data­base Deletes all users who are stored in a database.
usersInfo Shows all available user in­form­a­tion.
up­dateUser Updates the user data.
grantRo­lesT­oUser Gives a user certain rights or roles.
re­vokeR­oles­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
creat­eR­ole Creates a role and defines its rights and duties.
rolesInfo Queries the spe­cific­a­tions of a role.
up­dateR­ole Updates a role and the existing in­form­a­tion.
dropRole Deletes a specific role.
dro­pAll­Roles­From­Data­base Deletes all roles in a database.
grant­Priv­ilegesToRole Adds clearly defined priv­ileges to a role.
re­voke­Priv­ileges­From­Role Removes in­di­vidu­al priv­ileges from a role.
grantRo­lesToRole Defines roles whose priv­ileges are passed on to another role.
re­vokeR­oles­From­Role Removes inherited roles.
in­val­id­ateUser­Cache Clears the user cache and removes in­form­a­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.
find­And­Modi­fy Displays and modifies a specific document.
getMore Outputs documents which are selected with the cursor.
get­Las­tEr­ror Displays the status of the last operation performed.

How can I group and sort?

The database offers ag­greg­a­tion commands to sort documents ef­fi­ciently. 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­term­ines 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­tic­a­tion. You should know the following commands to increase security in MongoDB:

MongoDB commands De­scrip­tion
au­then­tic­ate Starts an au­then­tic­ated 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
startSes­sion Starts a new session.
re­fresh­Ses­sions Updates inactive sessions.
end­Ses­sions Ends sessions before the estimated time.
kill­Ses­sions Stops specified sessions.
kil­lAll­Ses­sions Stops all sessions im­me­di­ately.
kil­lAll­Ses­sions­By­Pat­tern Stops all sessions that match certain defined para­met­ers.
com­mit­Trans­ac­tion Performs a trans­ac­tion.
abortTrans­ac­tion Cancels a trans­ac­tion.

What are other ad­min­is­trat­ive commands?

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

MongoDB commands De­scrip­tion
cloneCol­lec­tionAs­Capped 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­vertTo­Capped Converts a col­lec­tion without a cap to a col­lec­tion with a cap.
cre­ateIn­dexes Adds one or more indexes to a col­lec­tion.
get­Para­met­er Displays con­fig­ur­a­tion options.
listIn­dexes Lists all available indexes in a col­lec­tion.
set­Para­met­er Modifies con­fig­ur­a­tion options.
shutdown In­ter­rupts the mongod or mongos process.

Diagnosis and mon­it­or­ing

MongoDB also provides commands for mon­it­or­ing and dia­gnostics. The following commands are helpful for carrying out these processes:

MongoDB commands De­scrip­tion
dbStats Provides stat­ist­ics on the type and load of a par­tic­u­lar database.
features Lists all available features.
server­Status Returns the status of the server in use.
buildInfo Shows all available in­form­a­tion about the current build of MongoDB.
con­nec­tion­Status Provides in­form­a­tion about the current con­nec­tion.
dataSize Shows the size of a file or a selected set of different files.
set­Free­Mon­it­or­ing Use this command to allow or disallow free mon­it­or­ing during runtime.
Go to Main Menu