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.
MongoDB " "
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.
|1||--help||Lists all the available options that can be used while starting up the MongoDB Shell.|
|2||--nodb||Specifies to start the MongoDB shell connecting to any database.|
|3||--shell||Specifies to start the shell after running any specific *.js files earlier.|
|4||--version||Specifies the version information of the MongoDB shell during startup.|
|5||--quiet||Starts the MongoDB shell with not many chatty messages.|
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 Commands||Description|
|1||help||Shows help related information on the MongoDB shell.|
|2||db.help()||Shows help related information on the database methods.|
|3||db..help||can be an existing collection or not, but provides help related information on database collections.|
|4||show dbs||Lists all the databases available for use on the connected MongoDB instance.|
|5||show databases||Lists all the databases available for use on the connected MongoDB instance.|
|6||use||Specifies the MongoDB shell to switch to the database provided with the parameter, switches the shell parameter (db) to it.|
|7||show collections||Lists all the collections available for use on the current database.|
|8||show users||Lists all the users available on the current database.|
|9||show roles||Lists all the roles (both built-in roles and user-defined roles) on the current database.|
|10||show profile||Lists the last 5 recent operations that took 1 millisecond or more.|
Subscribe to our youtube channel to get new updates..!
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 Method||Description|
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.
|2||db.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.
|3||db.fromCollection.renameCollection(toCollection)||As suggested from the method name, the collection named ‘fromCollection’ is being renamed to ‘toCollection’.|
|4||db.repairDatabase()||This command repairs and compacts the current database. This operation can be very slow if it is performed on a huge database.|
|5||db.getCollectionNames()||Provides a list of collections available in the current database as a list.|
|6||db.dropDatabase()||This command drops the current database.|
4. MongoDB Commands Listing (Basic Shell JS Operations):
|S.No.||JS Database Operations||Description|
|1||db.auth()||Authenticates the user, if the MongoDB shell is running in a secure mode.|
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
Finds all the documents in a specific collection and returns a cursor
From the example above, myCollectionVariable.find()
|4||db.collection.insertOne()||Inserts one document into the collection specified via the command.|
|5||db.collection.insertMany()||Inserts multiple documents into the collection specified via the command.|
|6||db.collection.updateOne()||Updates one single document in the collection specified via the command.|
|7||db.collection.updateMany()||Updates multiple documents in the collection specified via the command.|
|8||db.collection.save()||Inserts a new document into the collection if it doesn’t exists, and if it exists updates the document in the collection.|
|9||db.collection.deleteOne()||Deletes one document from the collection specified via the command.|
|10||db.collection.deleteMany()||Deletes multiple documents from the collection specified via the command.|
|11||db.collection.drop()||Drop or remove the collection entirely from the database|
|12||db.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.|
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.