MongoDB Basics for everyone – Part 4 Intro to the Mongo Shell

The commandline interface to MongoDB is a modified (and modifiable) JavaScript shell. This means that you can interact with the database using pretty simple Javascript. This also means that it is really quite simple to define your own javascript functions to do certain things if needs be.

You certainly don’t need to be a Javascript expert to work within the Mongo Shell, but some rudimentary knowledge will go a long way!

We will have a look at some of the more common commands used in the Mongo shell here, but I do encourage you to also have a look at the excellent docs at http://www.mongodb.org as well for further information.

We will start by opening up a terminal or command prompt and entering the Mongo shell with

mongo

This will give you a prompt showing the current version of Mongo shell installed and an information line saying that it has connected to a “test” database.

To display all the available databases, we use the command

show dbs

and it will list all available databases. In order to use a specific database, you will use

use <dbname>

. For now let us stay in the “test” database for our purposes now.

At any time whilst in the Mongo shell, you can use the built in help command. In order to see the available help use

db.help()

. This will display a list of available commands that you can use to interact with your database. As an aside, please note that if you omit the parentheses on the db commands, the shell will print out the function declaration, and not execute it! E.g. if we execute

db.stats

instead of

db.stats()

you will see:

> db.stats
function (scale) {
return this.runCommand({dbstats:1, scale:scale});
}

If we then go and execute the function as a function, we get:

> db.stats()
{
"db" : "test",
"collections" : 4,
"objects" : 18,
"avgObjSize" : 64,
"dataSize" : 1152,
"storageSize" : 24576,
"numExtents" : 4,
"indexes" : 3,
"indexSize" : 24528,
"fileSize" : 201326592,
"nsSizeMB" : 16,
"ok" : 1
}

Your stats will be different from mine, depending on how you have used Mongo up till now!

In order to check your server status, you can use

db.serverStatus()

which will print out a long JSON document all about the currently running process. It will give host info, as well as current read and write lock information on each of your databases. If you would only like to see more information about your host, you can use the

db.hostInfo()

command. Always take note of the

system.cpuAddrSize

field, as a 32bit architecture is a lot more limiting than a full 64bit system! You should always ensure that your mongo server is running on 64bit architecture to gain all the goodness that goes with Mongo!

For the purposes of this introduction, we will only look at one other shell command, and that is the command to drop a database. This command is obviously very useful when testing and exploring, so that you don’t end up polluting your Mongo server with a bunch of trash databases that are not useful to anyone!

To drop a database, you need to create one first. Create a database with the “use” keyword i.e.

use monkeys

which will output:

> use monkeys
switched to db monkeys

If you then insert something into a collection (types) in the monkeys db, you will see that it has been created and is working.

db.types.insert({"name":"Vervet monkey"})
db.types.findOne()
{ "_id" : ObjectId("51de563f2ceec8ed658e7221"), "name" : "Vervet monkey" }

Great! Now lets drop the monkeys db…

db.dropDatabase()
{ "dropped" : "monkeys", "ok" : 1 }

One last note! If you ever need to see which database you are currently working in, simply type the

db

command (without parentheses) and the shell will print out the database name.

I will do a further, more in depth exploration of the Mongo Shell later on, but that should keep you going for the time being!

One thought on “MongoDB Basics for everyone – Part 4 Intro to the Mongo Shell

  1. Pingback: The | 7Wins.eu

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>