Hurry! 20% Off Ends SoonRegister Now

MongoDB Commands

In this article, we will try to briefly discuss the commands available with MongoDB version v3.4.7 (which is the current stable version of MongoDB as on date of this article write-up) and this reference might not completely hold well with the earlier versions of the future versions of MongoDB.

If any of the commands seem not to work, it is highly suggested to go through the Official MongoDB documentation or cheat sheets specific to the versions of MongoDB that you are going to work with.

If you want to enrich your career and become a professional in MongoDB, then visit Mindmajix - a global online training platform: "MongoDB TrainingThis course will help you to achieve excellence in this domain. 

MongoDB Commands Cheatsheet list :

1. 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 Interview Questions for Beginners

 

MindMajix Youtube Channel

2. 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.

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.

Checkout MongoDB Tutorial

3. MongoDB Commands Listing (Administrative Command Helpers):

The sections above provide helpful commands that can get you online on MongoDB and do some administrative tasks on MongoDB. This section specifically works upon the administrative tasks on databases created within the MongoDB database server. The most commonly used commands are presented below in the table, take a look:

S.No.JS Database Administration MethodDescription
1db.cloneDatabase()

This command helps you to clone the current database from the mentioned in the command.

The only requirement for this to work is to have a database instance in a noauth mode.
2db.copyDatabase(, , )

This command helps you to copy the database provided in the clause from the clause to the database on the current server.

The only requirement for this to work is to have a database instance in a noauth mode.
3db.fromCollection.renameCollection(toCollection)As suggested from the method name, the collection named ‘fromCollection’ is being renamed to ‘toCollection’.
4db.repairDatabase()This command repairs and compacts the current database. This operation can be very slow if it is performed on a huge database.
5db.getCollectionNames()Provides a list of collections available in the current database as a list.
6db.dropDatabase()This command drops the current database.

4. MongoDB Commands Listing (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 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 a 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 cursor

From 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.
 

Checkout MongoDB Blogs

Conclusion:

This article provides a one-stop-shop for all the administration related queries, help related commands, and the basic CRUD operations that can be done on collections on a MongoDB database. As mentioned earlier during the introduction of the article, this is written for the latest stable release of MongoDB (version v3.4.7). The commands provided here may not work on other versions of MongoDB considering that there are many changes from the previous versions to the latest ones.

Hope this article helps you in getting the basic gist of the much-needed commands in using MongoDB in your day to day tasks.

 

Job Support Program

Online Work Support for your on-job roles.

jobservice

Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:

  • Pay Per Hour
  • Pay Per Week
  • Monthly
Learn MoreGet Job Support
Course Schedule
NameDates
MongoDB Training Dec 24 to Jan 08View Details
MongoDB Training Dec 28 to Jan 12View Details
MongoDB Training Dec 31 to Jan 15View Details
MongoDB Training Jan 04 to Jan 19View Details
Last updated: 03 Apr 2023
About Author

Prasanthi is an expert writer in MongoDB, and has written for various reputable online and print publications. At present, she is working for MindMajix, and writes content not only on MongoDB, but also on Sharepoint, Uipath, and AWS.

read less