MongoDB Admin Cheat Sheet

Connect with

If you are mongoDB admin you are interested to get some infomation regarding admin things in one place. I tried to collect few of the admin command and syntax in one place , you know all the syntax and command name is very difficult to collect in one place becuase requirment set for all the admin are different. This page is about the cheat sheet of mongoDB for mongoDB Admin pespective. A mongoDB admin use to do some job or activities in his/her life on day-to-day basis. In the day-to-day life usually few of the commands not remember on that particular time few may remember. For a mongoDB Admin the important is knowledge, I think not important of remembering of syntax and command. Help for syntax and command can be pull from different sources like google, notes.

How to resync data from one node to another

Open a mongo shell to new-host and your replica set primary.
On primary: rs.add("new-host:port")
ASAP On new-host: rs.syncFrom("preferred-host:port")

Adding of Node in running replicaset

rs.add( { host: "", priority: 0 } )

How to grant privileges

db.grantPrivilegesToRole("internRole", [{rsource: {db:"", collection: "internlog"}, actions: ["insert", "update"]}]);

db.getRole('internRole', {'showPrivileges': true});

How to Revoke privilege from any role?

– logged into mongo shell
– switch to admin database
– build resource and revoke privilege by using db.revokePrivilegesFromRole () function
– get role and its privilege by using db.getRole() function

use admin;
db.auth('userName', 'secret');
db.revokePrivilegesFromRole('internRole', [{resource: {db:"", collection: "internlog"}, actions: ["update"]}])

db.getRole('internRole', {'showPrivileges': true});


Which of the following is the correct function to revoke a privilege from a role?
Choose the best answer

a. db.revokePrivilegeFromRole
b. db.revokePrivilegesFromRole
c. db.removePrivilegeFromRole
d. db.removePrivilegesFromRole


The correct function to revoke a privilege from a role is db.revokePrivilegesFromRole.

How to add node in mongoDB replicaset as hidden?

> rs.add({_id: , host: "hostname:port", priority: 0, hidden: true});
//for example
rs.add({_id: 2, host: "", priority: 0, hidden: true});

What is hidden node in mongodb replicaset?

Language (java, C#, php etc) driver will not aware about hidden node, if you configure hidden node in mongodb replicaset. And hidden node not receive request from application. it is being used for reporting or backup purpose.

How to change hidden node with existing node?

> cfg = rs.conf()
cfg.members[2].priority = 0
cfg.members[2].hidden = true

// forcefully but be careful for production 
rs.reconfig(cfg, {force:true})

How to change priority of replica set?

First of all, you need to understand why you need to understand the priority? priority is used to change to remain a node primary if that is available else , primary is not available then another become primary basis on voting. By default priority of node is ‘1’ when you add. you can change this by following ways by logging on primary mongos

> cfg = rs.conf()
> cfg.members[0].priority = 5
> rs.reconfig(cfg);

How to change priority of replica set?

if you have to change host from ip to hostname or hostname to ip at any point of time you can do by following ways. in host you can pass either ip or hostname as per your need.

> cfg = rs.conf()
> cfg.members[0].host = ""
> rs.reconfig(cfg);

How to connect mongos if auth enabled?

If you have enabled the auth, then you can logged in mongos by following ways. logged-in into mongodb primary server/secondary server or standalon server.

mongo -u "username" -p "pXXXworXX" --authenticationDatabase "admin"

or use following on primary

> use admin;
> db.auth("pbadmin","pbadmin@123");

How to restore your data (.bson) if auth enabled?

If you have enabled the auth (authentication , then you can restored by following way.

mongorestore -d dbname -c collName /tmp/dbname/collName.bson -u "username" -p "pXXXworXX" --authenticationDatabase "admin"

Note: -c collName is the name of collection name, -d dbanme is your dbname where you want to store, -u username with dbadmin right.

Connect with

Leave a Reply

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