MongoDB Tutorial – Getting Started with MongoLab

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

This is a very basic MongoDB tutorial that will show you how to create a MongoDB database, add user, add document, and delete a database.  We will be using MongoLab as our database provider and host because it is one of the providers that has a free sandbox version (at least as of this writing in January 2014).  Mongodb is nosql database, so it will have slightly different terminology than your traditional SQL database, as you shall see.

Creating a new MongoDB database in MongoLab

1. After you log into your MongoDB account, click the “Create New” button.

2. Select a cloud provider and a location.

create new mongodb

create new mongodb

3. If you are just learning the system and want to use the free sandbox version mongolab (available at the time of this writing in January 2014), make sure you select the tab “Single-node (development)” and then select “Sandbox”.  In the sandbox version, there is not choice for storage option.  You just get half a gigabyte and that’s it.

4. Come up with a database name.  For this tutorial, we use “firstdb” as our first database.

5.  It is nice enough to show us the price for this setup which is $0/month.   Click button “Create new MongoDB Deployment”.

6.  You get a success message and see that your firstdb database is listed…

first mongodb created

first mongodb created

7. Click on the database name to see the database detail page.

Create a MongoDB user

8. In order to connect to the MongoDB database, you need to create a mongodb user.  In the database details page, click on the “users” tab and click the “Add database user” button…

creating mongodb user

creating mongodb user

9. Enter database username (in this tutorial we will use “firstuser”) and the password that you come up with.  The new database user is created.

Creating a Collection in MongoDB

In the database details page, click the Collections tab and click “Add Collection”.  Give it a collection name of “people” because we will be storing people names and ages.  And submit.

create new mongodb collection

create new mongodb collection

You will now see your newly created collection listed.  Click on that collection to go to the collections page where you can add a data by clicking the “Add Document” button …

adding mongodb document

adding mongodb document

A mongodb document is a piece of data to be stored — think of it like a row in a traditional SQL database.

In the create document window, enter as shown …

create document

create document

and click the “Create and go back” button.   You have just enter the data Alice of age 40 into the database.  Note that the syntax is JSON (javascript object notation).   It is an javascript object marked by curly braces.  Inside the object are key-value pairs separated by commas.  The key and value are both in quotes.

You now see your data listed.  You can list your data in list or table view.  We just work with list view for now, because the table view requires a bit of setup.

You will notice that mongodb has added an “_id” field to your object.  This field is always required.  Since we did not specify one, mongo created and _id field for us with an unique value.

You can try adding more documents.

Connecting to MongoDB using the MongoDB Shell

Two ways of connecting to the MongoDB database: one is via the MongoDB shell and another is via your web application.  For this tutorial, we will connect using the MongoDB shell.  So we will have to install MongoDB shell by downloading it from and follow the installation instructions for your particular operating system.

For this tutorial, I installed it at c:\Program files\mongodb_2.4.8\ in Windows.

Open up a command prompt and navigate to that directory.

Notice that in the database details page in MongoLab in the screenshot shown above, they gave us the mongo command for connecting to the database.  Type that command into the command window now.  It starts with the command “mongo”, the domain, the port, the database name, and then the username and password.  I know it is a long command.

The username and password is not your MongoLab username and password.  It is the username and password you had just created for the firstdb database.  The username in our tutorial is “firstuser”

Once connected, you can type the following commands. And other commands listed in mongo shell quick reference.

mongo shell commands
mongo shell commands

The “db” command will show the currently connected database.  The “show users” command shows the users created for this database.  The “show collections” command will shows the collections in the database.  A collection is like a table in the database.

The command …


will display the documents within the “people” collection.

To delete a mongodb database

In mongolab, select the database and in the database details page there is a “Delete Database” in the upper left for you to click.  There will be a delete confirmation for you to confirm that you really want to do this.