Friday, July 31, 2020

Useful Mongo DB commands

Here are some very important , useful and commonly used mongodb commands in database

show dbs ->to show all the databases in mongo
use <db>                     ->to switch to the selected database
db                             ->to Show currently connected database
show collections         ->to Show all the collections(tables) in currently connected db
show users                 ->to Show all the users in currently connected db
help                         ->to show basic help
db.serverStatus()         ->to Show database info and stats
db.getProfilingStatus() ->to check Profiler status
db.shutdownServer() ->to Manually shutdown db
db.logout()                 ->to Log out from current user
db.Order.findOne() ->to find one random document in "Order" collection
db.Order.find().pretty() ->to find all documents in the "Order" collection using nice formatting.
db.Order.findOne({'firstName':'Aryan'}) ->to Find one document in "Order" collection where firstName is Aryan
db.Order.find().sort(empId:1)                          ->to find all documents in "Order" collection and Sort by key "empId"  in ascending (1) or descending (-1) order
db.Order.find().limit(5)                                 ->to find 5 documents in "Order" collection
db.Order.find().explain()                         ->to show execution plan for find query
db.Order.find().help()                                 ->to show help on collection methods
db.Order.->totalIndexSize() ->to find Index size
db.Order.getIndexes()                   ->to find Index details of a collection "Order"
db.Order.createIndex(keys, options) ->to Create an ascending Index on "Order" collection with the index field
db.Order.reIndex()                                         ->to Rebuild all indexes on a "Order" collection
db.Order.dropIndex( { "empId" : 1 } )         ->to Drop index on "order" collection  on "empId" field

rs.conf()                     ->to shows the replica set configuration 
rs.freeze()                     ->to Prevent the current member from seeking election as primary
rs.status()                     ->to show Current Status of Replica Set
rs.reconfig()                 ->to Reconfig the Replica set  using the new configuration
rs.stepDown()                 ->to change current primary member of replication set to secondary and forces an election.

db.auth('username','password') ->to validate credentials of user

db.system.profile.find().limit(12).sort( { ts : -1 } ).pretty()     ->to show 12 most recent queries from profiler
db.system.profile.find( { op: { $ne : 'command' } } ).pretty() ->to show queries except command
db.system.profile.find( { ns : 'test.documents' } ).pretty() ->to show queries against particular collection
db.system.profile.find( { millis : { $gt : 6 } } ).pretty()                 ->to show queries slower than 6 sec
db.currentop(   {     "active" : true,     "microsecs_running" : { "$gt" : 200000 }})     ->to show active queries running longer than 200ms


Hope you will find above mondb commands useful.