Crash Course: NoSQL & mongoDB

Demystifying NoSQL and MongoDB.

By: Ramamohan NukalaPrincipal Consultant, ennVee

I love DBMS and out of curiosity I learned SQL in my college days. But now, in recent times, there is a lot of buzz in the market about NoSQL. The first time I heard NoSQL, I though, “what is “No SQL”?“, and “where can I go if SQL is my primary skill set?“. After browsing through Google I learned that NOSQL is actually “Not Only SQL Database.” (fun fact: it’s still pronounced as it spells “No SQL”).

What is NoSQL?

As mentioned above, NoSQL stands for “Not Only SQL Database”. Before we understand NoSQL, we need to shed some light into RDBMS.

What is RDBMS?

The most popular database model is Relational, also known as “RDBMS”, or Relational Data Base Management Systems. A relational database management system (RDBMS) is a collection of programs and capabilities that allows IT teams and others to create, update, administer and otherwise interact with a relational database. From the time it evolved, it is the most used model for the most successful databases.

RDBMS has its own rules to say a database as Relational database. These rules have always been a highlight of RDBMS, and RDBMS will have schema and a structure for data to be stored.

RDBMS Drawbacks

While the RDBMS model has many advantages, it also has a few disadvantages.

Structure of the data – some view this as a disadvantage (in my opinion, it isn’t a drawback).

Performance over large amount of DB.

If you are familiar with SQL, how many times have you received the error; “Character can’t be converted as Number.” or were fooled by a Null as a value?

Enough background on RDBM. Let’s move on to NoSQL.

NoSQL Databases

NoSQL databases essentially evolved to overcome the pitfalls of regular RDBMS. There are different models of data storage and they are categorized as either Document Databases, Key value Databases, Column oriented Databases, etc. I will emphasize the Document Database and mongoDB.

Document Databases store data as documents.

After completing a course on the basics of mongoDB, I noticed a few key points. The mongoDB will have DBs and collections in it, and a collection will have documents in it. If we compare that to our regular DBs, collection is a table and document is a record in the table.

The documents are stored as JSON type in mongoDB. For example,

Compass

Compass is the Graphical User Interface (GUI) used to access the mongoDB. The GUI has different sections and one of the most useful is the schema view. The schema view allows us to see the overall schema definition. There is also the documents view, where we can see the document. mongoDB has it own query language to access the database and can be used to CRUD (Create, Read, Update, and Delete).

Screen shot of Compass

A Few Interesting Points

If you do not have a value to insert for a particular column, do not mention that column at all while inserting it. Nothing great, ah. If we see in the DB after inserting the document, the column definitions themselves will not be there, meaning the structure does not need to have all of the columns.

What about querying the DB for a document that doesn’t have a particular field?

In the example below, movieDetails is the collection where mpaaRating is the field in it. We want to query the documents where this field does not exist at all. Here’s how:

db.moviesDetails.find({mpaaRating: {$exists: false}})

This is how you query it from mongo. Shell can be accessible from the command prompt after installing the mongoDB compass and the same can be queries through the GUI.

The first part, db, mentions the database

The next one, movieDetails, is the collection

find is the method for querying it

mpaaRating is the field

$exists is the operator for checking whether that particular field is there

false means it will return the documents that do not have that field

What if we want to query based on a particular field where it contains a null value?

Here’s how:

db.movieDetails.find({mpaaRating: null})

This will return the documents that not only have a null value in the field, but that do not have the field itself.

We typically have a data type associated with a column, but what if you provide a data type other than the actual defined one to store the data? The answer is yes, it’s possible. It has the flexibility and a particular field can have different data types.

It can also have nested documents and array types as fields in it.

Advantages of mongoDB

MongoDB is useful for larger databases and the performance is spectacular. During one of my last courses, The Basics of mongoDB, they used some lakes of data as a sample DB, and this is excellent. To maintain the higher availably of the database they have multiple replicas of the same database on different clusters. These clusters are provided by monoDB for which they charge.

Closing Thoughts

mongoDB is easy to scale and also among the most popular NoSQL databases currently available. It can be found in many large organizations including The Weather Channel, The City of Chicago, Bosch Software Innovations, Chico’s FAS, Inc.

There is no specific schema structure to follow in mongoDB and no complex joins are required to retrieve data.

Content management, Delivery, and Data hubs mongoDB can also be used in Big Data.