MongoDB Admin Cheat Sheet

Connect with

MongoDB AdminMongoDB admin cheat sheet for MongoDB admin user. The list of MongoDB admin queries for MongoDB Admin user.

1. Overview of MongoDB Admin cheat sheet

If you are a MongoDB admin, this article is more essential for you. You can get more information regarding MongoDB admin things in one place. I tried to collect a list of admin commands and syntax in this article. You may know a few of the syntax and command names but it’s difficult to collect in one place because the requirement set for all the admin is different. This page is about the cheat sheet of MongoDB from the MongoDB Admin perspective. A MongoDB admin uses to do some job or activities in his/her life on a day-to-day basis. In 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 syntax and command. Help for syntax and command can be pull from different sources like google, notes.

2. 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: "mongodb1.mysoftkey.com:27017", priority: 0 } )

3. How to grant privileges

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

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

4. How to Revoke privilege from any role?

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

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

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

5. Quiz on MongoDB admin

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

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

6. 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: "mongodb2.mysoftkey.com:27017", priority: 0, hidden: true});

7. What is hidden node in mongodb replicaset?

Application developed in any Languages e.g. java, C#, php etc, mongoDB database driver will not aware about hidden node added under mongoDB replicaSet, if you have added any hidden node in mongodb replicaset. So, hidden node never receive request from application. It is being used for reporting node (where run analytical queries) or delayed backup purpose.

8. How to change hidden node with existing node?

For changing hidden to the normal node, you have to set the hidden parameter value as true.

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

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

9. 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);

10. 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 = "10.0.0.23"
> rs.reconfig(cfg);

11. 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 standalone server. This is one of the task of cheat sheet MongoDB,which you perform daily basis multiple times.

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

or use following on primary

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

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

If you have enabled the auth (authentication , then you can restored by following way. This is one of the part of cheat sheet MongoDB.

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.

13. Reference for MongoDB Admin

You can visit docs.mongodb.com for more details regarding MongoDB Admin.

Thanks for visiting this post for MongoDB Admin Cheat Sheet for MongoDB admin queiries. You can also visit MongoDB Tutorial Listing page for more articles on MongoDB document-oriented datbase.
Happy Learning 🙂 for MongoDB Admin related task and its queries.


Connect with

Leave a Comment

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