What’s new in MongoDB Shell 2.2.2

I really suffered while using MongoDB Shell because auto completing was not there. I was very hard to type collection and database names correctly. I wished IntelliSense to get into there. I’m tough Microsoft User. Because of that I love IntelliSense a lot. 😉

And there was no any other solution to me to make done MongoDB things easier.  Because available MongoDB GUI’s are not that much good. I feel I’m in 90’s when I’m using them. So after a month I got addicted to Shells. Not Only to mongo shell I practiced PowerShell also.


Now what I wished is arrived in a different form. It’s auto completing. And it made me comfortable because feeling is same like using PowerShell. And also It suggest possible commands and functions. Isn’t it awesome? It makes easier to learn MongoDB. Somehow now MongoDB is having visual differences!

Go Mongo!!!

How to shutdown MongoDB instance from MongoDB Shell?

For some cases we may have access to database instance of MongoDB as DBA. But we may not have access to Linux or Windows box to shutdown the MongoDB service or MongoDB instance. To Shutdown the MongoDB instance you should be able to log in to “admin” database.  If your MongoDB instance is running with “auth” mode you really need a password and username. In this demo I assume MongoDB instance is not running with “auth” mode.

I login to MongoDB shell first.


Then I change the database to admin database.

use admin

Now I’m going to run that command. This command will shutdown the instance hence think twice before you use it.

db.runCommand( { shutdown : 1 } );

That is like adventure to a DBA because your database will crash if you don’t have any replication plan. 😉

How to copy a Database from a MongoDB instance to another?

Some times we need to take backups or we need to copy databases to another servers for administrative purposes. But sometimes just copying files is not enough. In MongoDB Shell they support to copy database from remote instance to current one with a single command. ( 😀 Just like Single Click in Windows )

For this demo I made alive two instances of MongoDB from following commands.

//Instance 1
mongod --port 9990 --dbpath /data/db1
//Instance 2
mongod --port 9991 --dbpath /data/db2

In instance 1 there is a database called “csampledb1”. For that I need to connect to first instance to create that database.

mongo localhost:9990


After that with following commands I create a database with one collection.

use csampledb1
db.csamplecol1.save({id:1, name:"sample name"})

Then I log in to next MongoDB instance using MongoDB Shell.

mongo localhost:9991

Then I use single command to copy the database from instance 1 to instance 2.



Syntax of this function is like below. There are two arguments which I didn’t use.

db.copyDatabase(fromdb, todb, fromhost, username, password)

MongoShell Database Navigation

If you are new to MongoDB you may need to discover the databases and collections (basically objects) on your MongoDB instance. For this easily you can use a GUI tool like MongoVUE. But in this blog post I’m not going to describe about GUI tools. I’m going to explain about MongoShell to navigate through database objects.

I can remember the first day that I used Linux. In that day I fed up with Terminal and gave it up. But now I think its cool!. Smile Some how if you want to do some thing easier I still recommend GUI Tools Winking smile.

First to execute those commands you should log into MongoShell. In windows mongo.exe.

How to take database List?

> show dbs

How to check the database that you are currently using?

> db


> print ( db );

How to change to a new database?

> use <database name>


> use AdventureWorks2012 

Winking smile

How to take the list of Collection in current Database?

> show collections


> db.getCollectionNames();

How to take the list of Users in Database?

> show users


> db.system.users.find();

How to take list of databases in SQL Server

In SQLCMD and Powershell I wanted to take the list of databases. In MySQL “show databases” command was there. But in SQLCMD I was unable to find such a command.

--Stored Procedure EXEC sp_databases



--SELECT Statement SELECT Name FROM master.dbo.sysdatabases


You can use above commands in SSMS (if you are lazy to move your mouse to object explorer) and SQLCMD. Also you can use same thing on PowerShell too.

--Stored Procedure
Invoke-SQLCMD "EXEC sp_databases"

--SELECT Statement
Invoke-SQLCMD "SELECT Name FROM master.dbo.sysdatabases"

But in SQLPS you can go to your SQL Server instance’s database and just type “dir”.

I want nice output from Mongo Shell

If you have so many documents in your collection in MongoDB database. Sometimes you may want to retrieve few documents. But if we run find() we cant have nice output. You may need to have nice indentation and line breaks. You may want results in more readable way. Then you can use pretty().


Who can read this??


If you use pretty() you can take nice output with Mongo Shell.