MongoDB Commands Cheat Sheet – Learning Guide
MongoDB Commands Cheat Sheet

MongoDB Commands Cheat Sheet – Learning Guide

Last updated on 28th Jun 2020, Blog, General

About author

Bibin (Lead Data Engineer )

He is a TOP-Rated Domain Expert with 11+ Years Of Experience, Also He is a Respective Technical Recruiter for Past 5 Years & Share's this Informative Articles For Freshers

(5.0) | 19212 Ratings 1957

There are many GUI tools to connect to MongoDB databases and browse but sometimes you need to get to the command line. That moment when something has gone wrong on the database server, and you need to SSH 4-levels deep in order to debug a problem with a database can leave you stuck.

In this cheat sheet you can get a quick refresher on:

  • Starting the mongo shell
  • Navigating around Mongo
  • Working with a collection
  • Changing groups of documents
  • Working with individual documents and Indexes
  • Dangers to avoid
Subscribe For Free Demo

[custom_views_post_title]

MongoDB Commands Listing (Common Line Options):

There are numerous options provided by MongoDB but in this section, let us discuss the most common options of MongoDB when used in conjunction with the MongoDB shell startup, to work with MongoDB database server seamlessly.

S.No.OptionDescription
1–helpLists all the available options that can be used while starting up the MongoDB Shell.
2–nodbSpecifies to start the MongoDB shell connecting to any database.
3–shellSpecifies to start the shell after running any specific *.js files earlier.
4–versionSpecifies the version information of the MongoDB shell during startup.
5–quietStarts the MongoDB shell with not many chatty messages.

MongoDB Commands Listing (Command Helpers):

There are various command helpers available for the MongoDB shell (mongo). The table below talks about the most commonly used help commands.

Course Curriculum

Enhance Your Career with MongoDB Training By Experts Trainers

  • Instructor-led Sessions
  • Real-life Case Studies
  • Assignments
Explore Curriculum

S.No.Help Commands and CommandsDescription
1helpShows help related information on the MongoDB shell.
2db.help()Shows help related information on the database methods.
3db..helpcan be an existing collection or not, but provides help related information on database collections.
4show dbsLists all the databases available for use on the connected MongoDB instance.
5show databasesLists all the databases available for use on the connected MongoDB instance.
6useSpecifies the MongoDB shell to switch to the database provided with the parameter, switches the shell parameter (db) to it.
7show collectionsLists all the collections available for use on the current database.
8show usersLists all the users available on the current database.
9show rolesLists all the roles (both built-in roles and user-defined roles) on the current database.
10show profileLists the last 5 recent operations that took 1 millisecond or more.
11load()Executes a specified javascript file.

MongoDB Commands Listing (Basic Shell JS Operations):

Basic-Shell-JS-Operations

Apart from the above help options and command-line options, MongoDB provides a rich collection of Javascript API for database related operations. DB is the mongo shell variable that holds the current database that we are pointing to. The variable is reset to the when the command is used, until then it points to the test database.

Some of the commonly used Javascript operations are discussed in the table below.

S.No.JS Database OperationsDescription
1db.auth()Authenticates the user, if the MongoDB shell is running in the secure mode.
2myCollectionVariable = db.Assign a specific collection ‘myCollection’ to the variable ‘myCollectionVariable’, as shown in the example below:myCollectionVariable = db.myCollection;You can then perform operations on myCollection on the variable myCollectionVariable instead. See below for example
3db.collection.find()Finds all the documents in a specific collection and returns a cursorFrom the example above, myCollectionVariable.find()
4db.collection.insertOne()Inserts one document into the collection specified via the command.
5db.collection.insertMany()Inserts multiple documents into the collection specified via the command.
6db.collection.updateOne()Updates one single document in the collection specified via the command.
7db.collection.updateMany()Updates multiple documents in the collection specified via the command.
8db.collection.save()Inserts a new document into the collection if it doesn’t exists, and if it exists updates the document in the collection.
9db.collection.deleteOne()Deletes one document from the collection specified via the command.
10db.collection.deleteMany()Deletes multiple documents from the collection specified via the command.
11db.collection.drop()Drop or remove the collection entirely from the database
12db.collection.createIndex()This command creates a new index on the collection specified via the command, if it doesn’t exist. If the index already exists, then there is no effect of this command over the collection specified by the command.

Database

DescriptionCommand
show all databaseshow dbs
show current databasedb.getName()
switch(and create if doesn’t exit) to database ‘userdb’use usersdb
drop current databasedb.dropDatabase()
Mongodb Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

Collection

DescriptionCommand
show all the collection under current databasedb.getCollectionNames()
create collection ‘users’db.createCollection(“users”)
drop collection ‘users’db.users.drop()

Are you looking training with Right Jobs?

Contact Us
Get Training Quote for Free