Part 1 – Introduction

Welcome to the complete series on exploring NoSQL database.

In this part

In this part, I will introduce you to the NoSQL database, MongoDB and its features.

Preface

Web application development is heavily dependent on usage of a relational database to store informations in various tables. There are many SQL solutions available open-source as well as commercial versions from big giants like oracle/microsoft. Being easy to map with ER diagrams it became standard approach of storing a normalized data in various tables with their Primary / Foreign key relations. Application program manipulates the model using various SQL statements (directly / or using ORM ) to arrange the data in some kind of hierarchical representation.

Document-oriented (often termed as NoSQL) databases handle data in a different way as key value store. They store documents in string formats usually as JSON and XML.This way of storage will allow better throughput with faster read operations since application using the data will not have to fetch multiple objects every time.

While developing some application, we often face problem of changes, mainly in data schema (data model). Let’s assume you want to add a new property to each blog post. As normal practice we do change the related table with new field. Existing objects / rows in the table will get either your preferred default values or any specific values provided. This might result in changes in the application for validations depending on application logic. Here comes the advantage of document-based databases: they are often schema less, where one can store different objects in a single collection without changing in existing database.

There are many NoSQL solutions available, the document-oriented databases are rapidly becoming industry standard. The ease of use, standalone persistent storage offering, faster read time are making document oriented databases obvious choice versus traditional RDBMS. Although there are a few document-oriented databases, none are as popular as MongoDB.

Introducing MongoDB

Back in 2007, Dwight Merriman and Eliot Horowitz formed a company named 10gen to create a better platform to host web applications. One of their project was a document-based database solution called MongoDB. Derived from the word humongous, MongoDB was able to support complex data storage, while maintaining the high-performance approach of other NoSQL stores. The community cheerfully adopted this new paradigm, making MongoDB one of the fastest-growing databases in the world.

Key features of MongoDB

MongoDB has some key features that made it so popular. It creates a bridge between traditional database features (SQL like queries) and the high performance of NoSQL stores.

1. The BSON format

One of the greatest features of MongoDB is its JSON-like storage format named BSON. Standing for Binary JSON, the BSON format is a binary-encoded serialization of JSON-like documents, and it is designed to be more efficient in size and speed, allowing high read/write throughput.

2. MongoDB ad hoc queries

Supporting ad hoc queries means that the database will respond to dynamically structured queries out of the box without the need to predefine each query. It is able to do this by indexing BSON documents and using a unique query language. Let’s have a look at the following SQL statement example:

SELECT * FROM Posts WHERE Title LIKE '%letsdoit%';

This simple example is fetching all the posts with a title containing the word letsdoit. Replicating this query in MongoDB will be as follows:

				
db.posts.find({ title:/letsdoit/ });

Running this command in the MongoDB shell will return all the posts whose title field contains the word letsdoit.

3. MongoDB indexing

To speed up the scan, the database engine can use a predefined index (default is _id), which maps documents fields and can tell the engine which documents are compatible with this query statement. To understand how indexes work, let’s say we want to retrieve all the posts that have more than 4 comments. For instance, if our document is defined as follows:

{
     "_id": ObjectId("53e05217e4g01d67d71ad577"),
     "title": "My firstPost",
     "comments": [
				
],					
    "commentsTotal": 5
   }

So, a MongoDB query that requests for documents with more than 5 comments would be as follows

			
db.posts.find({ commentsTotal: { $gt: 5 } });

If a commentsTotal index is defined, then MongoDB only have to check which documents have commentsTotal more than 5, before retrieving the results. If not defined index on commentsTotal it will scan all documents and return only satisfying the conditions.

4. MongoDB replica set

MongoDB uses an architecture called replica set. Replication of databases helps protect your data to recover from hardware failure and increase read capacity. A replica set is a set of MongoDB services that host the same dataset. One service is used as the primary and the other services are called secondaries. All of the set instances support read operations, but only the primary instance is in charge of write operations.

5. Automatic Scaling

Scaling is a common problem with a growing web application. The various approaches to solve this issue can be divided into two groups: vertical scaling and horizontal scaling.
Vertical scaling is easier and consists of increasing single machine resources,
such as RAM and CPU, in order to handle the load.

Horizontal scaling is more complicated and is done using several machines. Each machine will handle a part of the load, providing better overall performance.

In Summary

We have started with non traditional database in this post, with introduction to mongoDB knowing why its being so popular and widely used. Initially concurrency control was a key issue with document oriented databases, but later version of MongoDB 3.0 has introduced doc-level concurrency also.

In next part, we will discuss about how document schema helps building large scale applications and how we can use SQL like queries in MongoDB.

Sources: image from mongoDB https://www.mongodb.com/blog/post/announcing-mongodb-30

Content inspiration from https://www.packtpub.com/web-development/mean-web-development

Ravi Tiwari

Geeky mind, nerdy heart, father of 2 and occasional day dreamer ;)

Want to work with us? We're hiring!