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();

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.

How to install MongoDB as a Windows Service

At the first date I heard about NoSQL DBMS, I felt insane. Then I got an eager to learn. But I didn’t pay any attention to learn. Later I got to know that MongoDB is a scalable one. Then I got a chance to follow free MongoDB course. Although I registered to both Developer and DBA courses I had no time to watch developer tutorials. But I finished DBA 1st Week. In that video tutorial they explain how to install MongoDB on Windows as well as on UNIX. But when we are using Mongo in Windows Development Environment it’s a headache to start service again and again. Because of that I wanted to install MongoDB as a Windows Service.

In that video tutorial they explain how to just run MongoDB on Windows Environment. First of all you should have a data directory. For that you should create two folders in the drive that you are planing to install MongoDB. Lets assume that we are going to install MongoDB on D: Drive.

Then you should create two directories(Folders) in D: drive

  • D:\data
  • D:\data\db

You can manually change data path but for a beginner its better to use default path. And if you are trying to install it as Service you need to create a log folder.

  • D:\log

Then open a command prompt and go to the MongoDB bin. I assume you have pasted it on D:\ drive. your directory path will look like this D:\mongodb-win32-x86_64-2.2.0\bin.

After that you should run mongod.exe to install MongoDB as a windows service.

mongod.exe --install --logpath D:\log

When you have type and enter this command on CMD. It will create a Service for MongoDB. You can start MongoDB service with below command.

net start MongoDB

Enjoy Mongo!!!!