MongoDB is a part of a family of data-storage technologies called document databases; these are sometimes referred to as document-orientated databases, or document stores. In this article, we will take a look at how MongoDB uses documents to store, retrieve, and manage data.

Relational Data

Before diving into how MongoDB’s document model works, let’s take a quick look at how data is stored in a relational database.

In a RDMS, each database includes one or more tables. Within each table, data is organized into rows, and each row contains multiple cells (fields). The data within the tables is subject to a series or strict rules (constraints). Each table row must contain exactly the same number of fields. Each field must contain only a single item of data. The type of data the field stores (numbers, text, dates, etc.) must be the same as all the rows in the same column.

Tables can be linked to other tables through relationships, referred to as joins. All the rules and relationships in the database are collectively known as the schema.

Databases, Collections, and Documents

Like an RDMS, MongoDB also manages multiple databases, but the way it organizes data is very different. For a start, MongoDB does not use a schema, so there are less rules governing how data is ordered and stored.

As we dig deeper into Mongo, the differences between it and traditional databases become more pronounced. Instead of fixed table structures, Mongo databases store loose collections of data, called documents. These should not be confused with single file formats, such as Word documents. A Mongo document is a block of data formatted using a standard notation.

MongoDB formats its documents as BSON objects. This is a binary version of the popular Javascript Object Notation (JSON) web-standard. In both these formats, documents are indicated by a pair of braces {}. Within the braces, the document must include a key value pair. The key is an identifier by which Mongo can locate, organize, and manipulate the data. The value is the value stored in the document. The keys and values can be a number, text (string), or dates. In the following example, the document stores a single value, age; the key is a single word (age) and the value is a whole number (integer).

{ “age”: 40 }

A single value document is not very useful. Fortunately, MongoDB lets you create documents that include multiple key value pairs; with each key value pair separated by a comma. The following example shows a document that describes the protagonist of Jo Nesbo’s detective novels. The character’s first and last name, occupation, location, and age are all key value pairs.

{ 
 “firstname”  : “Harry”, 
 “lastname”   : “Hole”, 
 “occupation” : “Policeman”, 
 “location” : “Oslo”, 
 “Age”: 40 
}

Documents can also include complex data types, such as lists (arrays), and even other documents. In the next example, a list called weaknesses that describe Harry Holes’ major personality flaws. The document’s final block includes two embedded documents that describe some of his enemies.

{  
  “firstname”  : “Harry”,
  “lastname”   : “Hole”,
  “occupation” : “Policeman”,
  “location” : “Oslo”,
  “Age” : 40,
  “weaknesses” : [‘alcohol’,  ‘lost causes’],
  “enemies” : [
    {“alias” : “The Prince”, “occupation” :       “detective”},
    {“alias” : “The Snowman”, “occupation” : “serial killer”}]
}

Creating, Managing, and Viewing Daata

In addition to storing information, databases must also provide some means of creating, accessing and managing data. In a relational database, these tasks are handled by Standard Query Language (SQL). In MongoDB, these tasks are performed via a subset of Javascript. Mongo also gives you a command line shell through which to perform these tasks.

To illustrate, let’s walk through the process of creating a document and inserting it into the database.

Using MongoDB’s command line shell, you can set the database you want to use. If the database does not exist Mongo creates it:

use db

Since a database usually contains more than one collection, let see if there is a collection we can use:

show collections

In our database, this returns:

book
characters
system.indexes
things

Before we add a character to the characters collection, we first create a character.

character = {"firstname":"Harry","lastname":"Hole"}

Then, we add it to a collection called characters.

db.characters.insert(character)

Now that we inserted a character to the characters collection, let’s verify that it was actually part of the collection. First let’s display a list of all the characters in the collection with the find command:

db.characters.find()

This returns the following:

{ "_id" : ObjectId("5198df25805e7637d031c871"), "firstname" : "Rakel", "lastname " : "Faulke" } { "_id" : ObjectId("5198de7d805e7637d031c872"), "firstname" : "Harry", "lastname" : "Hole" }

Note that when you insert a document into a collection, Mongo automatically assigns it a unique ID.

If the characters collection contained many characters, we can locate a single character by their first name. For example:

db.characters.find({firstname:"Harry"})

This will return:

{ "_id" : ObjectId("5198de7d805e7637d031c872"), "firstname" : "Harry", "lastname" : "Hole" }