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.

mongo

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. 😉

Advertisements

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

or

> print ( db );

How to change to a new database?

> use <database name>

E.g:-

> use AdventureWorks2012 

Winking smile

How to take the list of Collection in current Database?

> show collections

or

> db.getCollectionNames();

How to take the list of Users in Database?

> show users

or

> db.system.users.find();