Once you have connected to mongod in your environment through mongo shell. You want to know about the databases or may be all collection in a particular database. You can use number of options while connecting to mongo but I prefer it in a simple way

mongo  –host yourhost –port 27017  -u username -p password 

*mongo commands and scripting language is case sensitive ~Be Carefull *

On successful login, you will be presented with mongo CLI.  Here are the 5 commands you must know

1): show

Use show command to get or print information about databases, collections, users, roles and profile

  • show dbs        — show list of database in mongo instance you connected to
  • show dbs         — show list of collection in current database you are in 
  • show users      — show list of user for current database
  • show roles       —show list of user defined or system roles in current database
  • show profiles   –show the five most recent operations that took 1ms or more

2) : db.serverBuildInfo()

It is very useful command (pretty much like @@Version in SQL Server world) which return a document that will let you understand about mongo version, build, edition and storage engines.

3) : db.currentOp()

Returns a document about  in-progress sessions for the database instance. You can see each session’s info like seconds running, query, lock etc. Here what I use very frequently,

db.currentOp(
{
“secs_running” : { “$gt” : 3},
“active” : true
}
)

4) : db.killOp

Terminates an operation as specified by the operation ID. Use db.currentOp() to find current operations and their corresponding IDs

5) : db.collectionname.find()

It is like querying a table in SQL world with SELECT. It will show the list documents in a collection.

 

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s