Useful MongoDb Commands Every SysAdmin Should Know

Useful MongoDb Commands For SysAdmin

In this article, I am going to give you some useful MongoDb commands which are used by SysAdmins in their daily work.

Before looking into useful MongoDb commands I hope you have install MongoDB to test these commands on your system. If you still not install mongoDb then check my previous article on Installation of MongoDB. check below link and learn simple steps to install MongoDB on a

If you still not install mongoDb then check my previous article on Installation of MongoDB. check below link and learn simple steps to install MongoDB on a Linux system.

Install MongoDB Community Edition on Debian/Mint/Ubuntu

Now let's start with the learning of Some most useful MongoDb commands which helps you in your daily work about MongoDB.

1. Start|Stop|Restart MongoDb

With this commands you can able to start,stop and restart MongoDB service on Linux.

$ /etc/init.d/mongodb start
$ /etc/init.d/mongodb stop
$ /etc/init.d/mongodb restart

2. Run MongoDb

To start working on mongo server use below command to get into mongo to use.

$ mongo   [if you are mongo is on same or local machine]
$ mongo --host Remote-server-ip [if you are mongo server is on remote machine]

3. Show All Databases

Use below command to check the available database on mongo server.

$ show dbs;

4. Switch or Create Database

You can switch or create a new database using below command. But make sure when you create a new database you need to add list single collection on that database.

$ use newdatabase

5. Check Collections

To check or show collections from any mongo database. first use that database then run below command to show all available collections on that database.

$ show collections

6. Create or Add data in MongoDB

If you want to add new data through mongo then use below insert command to add new data to the used database.

$ use newdatabase
$ db.testcollection.insert({name:"rahul", age:28})
$ db.testcollection.insert({name:"sameer", age:25})
$ db.testcollection.insert({name:"sameer", age:23})

7. Read Data from MongoDb

To read available data or entry's from the collection then use below find command.

$ db.testcollection.find()

{ "_id" : ObjectId("4e9ebb318c02bf412"), "name" : "rahul", "year" : 28 }
{ "_id" : ObjectId("4e9ebb478c02af413"), "name" : "sameer", "year" : 25 }
{ "_id" : ObjectId("4e9ebd5d8c00ef414"), "name" : "rohan", "year" : 23 }

Above command will show all the data from the collection. if you want provide a limit to check data from the collection then use below command.

$ db.testcollection.find().limit(2)

{ "_id" : ObjectId("4e9ebb318c02bf412"), "name" : "rahul", "year" : 28 }
{ "_id" : ObjectId("4e9ebb478c02af413"), "name" : "sameer", "year" : 25 }

8. Check Records in MongoDB

You can check available records in the database using below count command. It will give you a count of records or entry's available under the database.

$ db.testcollection.count({})

Above command will return the total n. of documents or records in a collection named testcollection.

9. Drop Database

To drop any mongo database first you need to use it then run the below-provided drop command.

$ use newdatabase
switched to db newdatabase

$ db.dropDatabase()
{ "dropped" : "newdatabase", "ok" : 1 }

10. Drop Collection

To drop collection from database use below command,

$ use newdatabase
switched to db newdatabase

$ db.testcollection.drop()

11. Dump or Backup MongoDb

This is a very important command which saves system admin life in a disaster situation. To take a dump of MongoDB database you need to use mongodump command. Use below command to take mongo database dump.

$ mongodump --host --port 27017 --db DatabaseName --out /data/DatabaseName

12. Restore MongoDB

To restore MongoDB from backup or dump mongo file then use below mongorestore command. This command will create a database with directory name and restore data. If you want to restore the database with a different name then just rename the directory name with requiring database name.

$ mongorestore --host --port 27017 /data/DatabaseName

These are the list of some important and useful MongoDB commands used by System admin in daily work. I hope you will find this article useful. if you have any queries regarding this article then feel free to comment your doubts in the comment box. Till then stay tuned and connected with for more such useful articles.

Thank You 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *