Posted on by & filed under Content - Highlights and Reviews, Programming & Development, Web Development.

SQL databases such as MySQL, Microsoft SQL Server and Oracle have served us well for the past decades. From small desktop applications to large enterprise resource planning systems, these databases have been faithfully fulfilling their tasks. The massive growth of web applications and cloud computing, however, presents new scalability challenges for which a new breed of databases referred to as NoSQL, or Not only SQL, have emerged. In this article we discuss one such database, MongoDB, which is leading this pack of new databases from the front.

Documents and collections

MongoDB is a document-oriented database. In traditional SQL databases, data is arranged in the form of tables and rows. Each row has a fixed number of columns (the schema) and each column can only store data of a specific type (e.g., Integer, Text, Datetime). This format allows data to be stored in a strictly rigid structure. In MongoDB, data is stored in the form of documents that are organized into collections. A document is similar to a JSON structure that doesn’t have a fixed schema. It is roughly equivalent to a row in a SQL database. Collections are used to group together similar documents and are thus equivalent to tables in SQL. For example, in MongoDB, data for users will be stored in the ‘Users’ collection, which can have the following documents:

The curly braces {} denote the start and end of the JavaScript structure (more specifically, JSON). There is no predefined structure for the documents. Each document can have any number of fields, with any name. For example, User 1 has ‘name’ and ‘age’ whereas User 2 has ‘name’, ‘company’ and ‘location’. Note that each document has a field ‘_id’. This is a special field that is present in all documents and is called the Object ID. The Object ID of each document has to be unique and is used to identify the document. You can specify any value for the Object ID but if left out, Mongo automatically inserts a unique value.

There is no support for ‘joining’ documents in MongoDB like SQL joins. The question arises as to how to store related data. The answer lies in nested documents. In MongoDB, a field can contain not only ordinary data such as Text, Integer, etc., but also another document! This enables documents to be nested in one another enabling complex relations to be stored. For example, modifying the above example we can store the address of User 1 as:

The ‘address’ field now contains another nested document. This document has its own fields ‘line_1’, ‘line_2’ and ‘city’. Using this nesting structure, Mongo allows the equivalent SQL relations of One-to-One and One-to-Many to be stored. Many-to-Many relations however require another technique which will be discussed in a later article.

How is MongoDB better than SQL databases?

While the rigid structure of SQL is acceptable for most applications, the rapid development cycles and quick iterations common today make the database migration steps a chore. Currently with systems using SQL, any change in data structure require schema updates followed by data migration procedures that are both time consuming and prone to errors. With MongoDB, no such migration steps are necessary since there is no fixed schema.

Moreover, the rigid schema of SQL limits the horizontal scalability in certain cases; an example is SQL joins involving tables spread across multiple servers. MongoDB’s architecture is designed to handle exactly these kinds of scalability issues. Each document is independent and stored completely on one server. There are no joins in Mongo and any relations have to be implemented using nested documents (or by implementing document joins at the application level). Auto-sharding is a built-in feature, enabling MongoDB databases to be horizontally partitioned across server clusters.

These, along with some other features, enable Mongo to handle massive scalability challenges encountered by Web Applications of today.

A quick tour

We will now briefly walk through the environment provided by MongoDB. In later articles, the environment and programming model exposed by MongoDB will be covered in more detail.

Start by installing MongoDB on your system. Navigate to and choose the option most suitable for your platform. Follow the instructions available for your platform at to configure and setup the environment. Once installed, open the Command Prompt (or Terminal) and run the mongo program:

By default the Mongo Shell opens the “test” database on localhost. If installed properly, the shell will be able to successfully open the database.

Let’s insert some data into the database. As explained previously, Mongo’s documents resemble JSON documents. In fact, the Mongo Shell is a full blown JavaScript interpreter. Hence, documents need to be inserted using the JavaScript notation. Type the following:

“db” refers to the currently selected database (in our case “test”). “users” is the name of the collection. You can use any name for the collection as long as you use the same name every time. “save” is the method used to insert data into the database. It takes one parameter names the document object. We have passed this object using the object literal notation of JavaScript. The curly braces denote the start and end of the literal, whereas the object properties are specified as a comma-separated list of key-value pairs with a colon in between.

To view the data saved, type:

Again, “db” refers to the current database and “users” is the name of the collection. “find” is the method used to retrieve data from the collection. It will retrieve all the documents stored in the collection. In our case, this will produce the output:

We can verify that the document we just stored has been output. One thing to note is the “_id” field. We did not specify any value for this field when we saved the document so MongoDB automatically inserted a unique value. Thus, even if all the other fields of the documents are the same, we can still uniquely reference each document individually.


We covered the document-oriented nature of MongoDB and how it can be used to store structured data. We briefly walked through the Mongo Shell and saw some examples of storing and retrieving data in MongoDB. In subsequent articles, we will cover in detail Data Modeling, Programming and Querying in MongoDB.

Safari Books Online has the content you need

Below are some MongoDB books to help you develop applications, or you can check out all of the MongoDB books and training videos available from Safari Books Online. You can browse the content in preview mode or you can gain access to more information with a free trial or subscription to Safari Books Online.

MongoDB in Action is a comprehensive guide to MongoDB for application developers. The book begins by explaining what makes MongoDB unique and describing its ideal use cases. A series of tutorials designed for MongoDB mastery then leads into detailed examples for leveraging MongoDB in e-commerce, social networking, analytics, and other common applications.
MongoDB and Python is a cookbook-style text to help Python programmers work with MongoDB. It is full of useful, practical recipes for solving real-world problems ranging from how to do fast geo queries for location-based apps to efficiently indexing your user documents for social-graph lookups to how best to integrate MongoDB with the Pyramid Web framework.
Learn how to create large MongoDB clusters! Scaling MongoDB shows you how to use MongoDB efficiently for very large databases. It Covers sharding, cluster setup, and administration.

About the authors

Salman Ul Haq is a techpreneur, co-founder and CEO of TunaCode, Inc., a startup that delivers GPU-accelerated computing solutions to time-critical application domains. He holds a degree is Computer Systems Engineering. His current focus is on delivering the right solution for cloud security. He can be reached at
Shaneeb Kamran is a Computer Engineer from one of the leading universities of Pakistan. His programming journey started at the age of 12 and ever since he has dabbled himself in every new and shiny software technology he could get his hands on. He is currently involved in a startup that is working on cloud computing products.

Tags: Big Data, Javascript, JSON, MongoDB, mySQL, NoSQL, Oracle,

Comments are closed.