Why Use Mongo?

MongoDB is arguably one of the most popular NoSQL database options available and it is completely open source. So what is it and why would you want to use it?

First, what is a relational database?

SQL in its many flavors is the most widely used relational database language. Some fun facts about SQL: it was created in 1970 by IBM and was originally called SEQUEL (Structured Query English Language) and was later shortened to just SQL (Structured Query Language) because SEQUEL had already been copyrighted by an aircraft company. SQL, by the way, reads a lot like plain english:

UPDATE dogs
SET total = total + 1
WHERE owner = "Andrew"

So if I had a database where the user, Andrew had many dogs associated with him and I wanted to increment that total by one, the SQL statement above would accomplish this. SQL reads a lot like English but what is it at a higher level? What is a relational database?

Data is organized into columns and rows based on relationships where each row has a unique identifying key. Check out this infographic for a more detailed explanation:

An infographic by IBM on relational databases (source)

The takeaway is that everything is stored based on associations. In my example above, a user, Andrew, can have many dogs. Therefore the relationship between a user and a dog is a user has many dogs and a dog belongs to a user. A row represents an instance of whatever type it is, like user or dog, and the columns represent values attributed to that instance like name or address. Another key aspect of a relational database is that you have to plan out your schema or relations beforehand and may even need to rework them as the database scales, which can become very complicated very fast. So relational databases are best used in situations where there are many relationships that you want to keep track of.

What is a MongoDB?

MongoDB is a NoSQL document-oriented database where. This means that you are dealing with JSON-like documents that are only concerned with storing data in one place, or document instead of worrying about relationships. Also, a fun fact about Mongo is that it comes from the word huMONGOus. Let’s compare two queries:

A SQL query to insert values into the users table
Inserting the same values into a users table and users document respectively (source).

There are no columns, tables or rows, instead it’s just a document with key/value pairs in a format very similar to JSON. The takeaway here is that all of your data is grouped by collections of documents, rather than being scattered across different tables. This makes scaling your database and making any changes extremely easy as there are no relationships that need to be reconfigured and there is no schema that has to be altered. Different documents can differ in size, content, and number of fields so this makes adding and removing data a lot easier as you are just dealing with key/value pairs. You can also query your database using built in Mongo queries or by writing custom functions!

Rather than worrying about storing data across different tables, everything is kept in one massive document. If your database does not rely upon a lot of relations, then MongoDB is most likely the way to go!

These are the high level differences between MongoDB and SQL. If you’re interested in finding out more, check out these resources:

A brief history of relational databases

MongoDB Documentation

MongoDB Advantages over SQL DB

Broad overview of NoSQL database models

Intro to MongoDB Video tutorial

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.