Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼


Getting Started with MongoDB

MongoDB is a widely used open-source NoSQL database (by some surveys, the most widely used). It offers a flexible, dynamic, document-oriented storage model that provides the high throughput and easy scalability required for projects with big data loads. MongoDB provides drivers and client libraries for the most popular programming languages, including C#. In this first article in a three-part series dedicated to developing a MongoDB application in C#, I explain what you need to know to set up and start working with MongoDB databases. In the next installment, I'll begin looking at access with C#.

Installing MongoDB on Windows

MongoDB supports the most popular platforms. However, as I want to cover a typical .NET development environment, I'll focus on using it on 64-bit Windows. You can make use of everything I'll explain the about the MongoDB C# driver when working with the database on other platforms, such as Linux.

If you still have doubts about why NoSQL databases have become popular in data centers in recent years, I suggest you read "Why NoSQL Is Here To Stay" or browse through "NoSQL Options Compared". This short article is a brief overview of the advantages of MongoDB over relational databases for big data loads. If you've been working for many years with traditional relational DBMSs, when you start working with NoSQL databases, you'll need to shift paradigms. In addition, you need to work with different tools to access and manipulate data. Thus, you have to forget a bit about the familiar world of SQL queries.

You can download the latest stable MongoDB version for 64-bit Windows. At the time of writing, the latest stable MongoDB production release is 2.2.3 (see Figure 1). Both the source code and the binaries are available for download. You will notice that the Windows 64-bit column includes two hyperlinks: download and *2008R2+. If you want to install MongoDB on 64-bit Windows 7 or 8, you must click on download and the browser will start downloading a zip file with the binaries. If you are working with Windows 2008 R2, you must click on *2008R2+. I will use 64-bit Windows 8 for my example of the installation process.

MongoDB distributions
Figure 1: The different MongoDB distributions by platform and version for the latest production release: 2.2.3.

Once you've downloaded the zip file with the binaries, there is no need to execute any installation to launch MongoDB in your development environment. You just need to unzip the downloaded file to your desired folder and create additional folders to hold the data files. For example, you can create a new mongodb folder and unzip the contents of the mongodb-win32-x86_64-2.2.3.zip file within that folder, without creating a mongodb-win32-x86_64-2.2.3 sub-folder. This way, you will then need to access mongodb\bin to find the main mongodb executable (mongod.exe) and the different tools.

By default, MongoDB uses C:\data\db as the data folder. Thus, if you want to use MongoDB without specifying additional configuration options, you have to create two folders: C:\data and C:\data\db. Then, you can launch a command prompt, go to the mongodb\bin folder and execute mongod.exe. In Windows 8, you just need to press Windows + X and select Command Prompt in the context menu that appears.

The first time you execute mongod.exe, a firewall alert dialog box will appear because mongod.exe works with two ports: 27017 and 28017. If you're using Windows Firewall, you just need to activate the "Private networks, such as my home or work network" checkbox and click "Allow access." Other firewall products will display different dialog boxes. The following lines show the information provided by mongod.exe after it starts:

mongod --help for help and startup options
Thu Feb 28 14:41:12 [initandlisten] MongoDB starting : pid=800 port=27017
dbpath=\data\db\ 64-bit host=WIN8X64
Thu Feb 28 14:41:12 [initandlisten] db version v2.2.3, pdfile version 4.5
Thu Feb 28 14:41:12 [initandlisten] git version:
Thu Feb 28 14:41:12 [initandlisten] build info: windows sys.getwindowsversion
(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1')
Thu Feb 28 14:41:12 [initandlisten] options: {}
Thu Feb 28 14:41:12 [initandlisten] journal dir=/data/db/journal
Thu Feb 28 14:41:12 [initandlisten] recover : no journal files present, no recovery needed
Thu Feb 28 14:41:12 [websvr] admin web console waiting for connections on port 28017
Thu Feb 28 14:41:12 [initandlisten] waiting for connections on port 27017

When you see the waiting for connections messages, it means that MongoDB has started successfully. If you take a look at the last two log lines, you will notice MongoDB will wait for connections on port 27017 and the administration Web console will do the same on port 28017. Go to http://localhost:28017 in a browser and you can view the administration Web console with hyperlinks for many commands (on top), statistics, and the log displayed when you initialized MongoDB (at the bottom); see Figure 2. For example, one of the commands is listDatabases.

The MongoDB administration Web console
Figure 2: The MongoDB administration Web console.

However, to make these commands work, it is necessary to stop MongoDB and then run mongod.exe again with the --rest command-line option that turns on the simple REST API: mongod --rest.

After following these steps, you can go back to http://localhost:28017 in a Web browser and click listDatabases. The browser will display the JSON result returned by the REST API that executed the listDatabses command, http://localhost:28017/listDatabases?text=1 (Figure 3).

MongoDB administration Web console listing the databases
Figure 3: The MongoDB administration Web console listing the databases.

All the commands generate a JSON result displayed in the Web browser. The following is the JSON result for an empty local database

{ "databases" : [ 
    { "name" : "local",
      "sizeOnDisk" : 1,
      "empty" : true } ],
  "totalSize" : 0 }

It is also possible to setup MongoDB as a Windows service in the event you don't want to run it from the command line. While this deployment is local, MongoDB is frequently deployed in the cloud and you can even find MongoDB deployments on Windows Azure.

Some Basic Concepts

Most modern RDMBS provide a shell that allows you to execute SQL statements and see the results of their execution. MongoDB provides an interactive JavaScript shell that supports tab completion. To start the MongoDB shell, you can launch a command prompt, go to the mongodb\bin folder and execute mongo.exe (Figure 4).

MongoDB administration Web console listing the databases
Figure 4: The MongoDB JavaScript shell.

The MongoDB shell establishes a connection to a default test database (which still does not exist). MongoDB creates things only when necessary, therefore, the test database will be created if you add and save elements to it. As soon as you establish a connection, you will see a new line in the log for the database server (mongod), such as the following:

[initandlisten] connection accepted from #1 (1 connection now open)

Before creating applications that interact with a MongoDB database, it is worthwhile to spend some time working with the MongoDB shell because it allows you to have a better understanding of the way MongoDB works. In fact, you will apply everything you learn working with the MongoDB shell in your future work through the usage of the C# driver. Because the MongoDB shell is a JavaScript interpreter, you can write code that performs complex tasks. For example, you can easily write for loops that add test data by taking advantage of your existing JavaScript knowledge.

MongoDB is document-oriented and uses the BSON (short for Binary JSON) format to represent documents. BSON is a binary-encoded serialization of JSON-like documents that includes the same document and array embedding capabilities found in JSON. BSON includes additional data types that aren't part of the JSON specification and are very important for MongoDB, such as Date and BinData (binary data). BinData is equivalent to a BLOB (short for Binary Large OBject) type in RDBMSs. If you want to dive deeper on the BSON spec, you can read its details at BSON website.

Each MongoDB document is a group of field and value pairs. A value might be any of the following:

  • A BSON type.
  • A document.
  • An array of any BSON type.
  • An array of documents.

Thus, a document can have nested documents (when the value is a document or an array of documents). These embedded documents are extremely powerful and provide great flexibility for the schemas, but they have to be used with care. In fact, the maximum document size (without using a special API called GridFS) is 16 MB.

MongoDB works with collections instead of tables, and each row in a collection is known as a document. Because documents contain a group of field and value pairs, the appropriate name to identify each document's property is obviously field, instead of the popular column name used when working with RDBMSs schemas. Every document includes a primary key that is automatically set to the _id field (an object ID).

Collections are document containers that don't impose any sort of schema (an important difference from traditional relational tables); therefore, each document might have different fields. It would be possible to have documents with completely different structures within a single collection. However, in real-life applications, the documents generally will have a basic common structure.

Redesigning a Relational Schema to Create a Document-Oriented Schema

When working with MongoDB, there is no need to design a structured schema from scratch. Each document can add the necessary information as needed. However, at minimum, you need to have an idea of the initial contents for the different collections that your database requires before you begin coding. I do believe one of the easiest ways to understand how MongoDB works is by taking an existing relational schema and redesigning it while taking advantage of MongoDB's exclusive features. This way, you start making the paradigm shift from something you know (a relational database) to something new (a document-oriented database).

I'll borrow a RDBMS schema with the entities and the relationships of a Retrogames database from the recent Dr. Dobb's article "Information-Rich Programming With F# 3.0." The database has the following entities (see Figure 5):

  • Game
  • GameCategory
  • Gender
  • Player
  • PlayerScore

Entities and their relationships for the Retrogames relational database
Figure 5: Entities and their relationships for the Retrogames relational database.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.