When it comes to developing our applications, one of the most important part is to choose the way in which we will organize our data. Or in other words, to establish a system which will allow us to store, manage and retrieve certain pieces of information. And it’s in this part of the developing process that programmers are now facing a new choice: to use SQL or NoSQL?
SQL vs NoSQL: What is the difference?
>For the specific purpose of this article, we are going to talk deeper about NoSQL databases, and in particular about MongoDB, which is one of the hottest solutions out there in the databases’ world for our applications.
But before diving into Mongo, we need to properly understand what we are talking about when we refer to NoSQL and the philosophy behind this technology. And the best way to ensure that is to compare it with SQL solutions!
The classics SQL databases that are very often used represent the result of years of technology innovation, developing of good practices, and very detailed testing processes. They were designed with a specific eye for reliable operations and ad-hoc queries.
But the more we go on, the more we develop new needs for our applications, and the limitations of such solutions start to arise. Problems such as rigid schema and issues with scalability that make them less suitable for other specific kinds of apps.
But let’s analyze SQL Databases a little bit deeper:
The primary concept behind SQL relational databases is the table. We will have one for each piece of information we will deal with. Each table will be composed of columns and rows, representing an aspect of data.
Each table has one or more columns to represent the primary key, this is a piece of information used to uniquely identify an instance of it. In a relational database, we can also establish relationships between tables, for example, a row of table A can refer to a row in table B.
The language used to work with SQL relational databases is SQL (structured query language). On one hand, this language can be extremely powerful, SQL is, in fact, one of the most versatile, used and safe options available in the market. And moreover, it’s especially great for certain tasks like complex queries.
On the other hand, this solution can become quite restrictive. Due to the core inner working of SQL databases, using this language will require you as a developer to use predefined schemas to determine the structure of your data before you work with it. And all of your data will have to follow the same structure.
So, understand how this can become quite complicated if you desire to make a major change to how you structure your information, that would require a lot of work and preparation.
Advantages of SQL Databases
Following there are some of the benefits of using SQL Databases:
- Open source and free
- Well tested technology, with a giant community supporting it
- Compatible with most of the platforms like Windows, Linux and Mac. You can also connect it with Node.js, Ruby, C#, C++, Java, Perl, Python and PHP, meaning that it’s not limited to SQL query language.
Field of use for SQL Databases
Whenever you want to benefit from the predefined structure and strict rules that SQL provides, then go with a SQL Database. Examples of applications especially suited for this type of problem are: Inventory Apps, Accounting Systems.
NoSQL databases arose during time as a response to the limitations mentioned above. NoSQL systems store and manage data in ways that allow for high performance, operational speed and great flexibility on the part of the developers for the structure of our data.
For these types of solutions, the whole pre-defined structure necessity that is a major characteristic of SQL relational databases is absent. Your data, won’t be stored anymore in tables but in objects that can be completely different and structured in totally different ways. Thank to this feature we can identify the most common types of NoSQL systems :
- Document databases (e.g. CouchDB, MongoDB). Inserted data is stored in the form of free-form JSON structures or “documents,” where the data could be of any kind ranging from integers to strings to plain text. There is no inherent need to specify what fields, if any, a document will contain.
- Key-value stores (e.g. Redis, Riak). Also here ranging from plain text values, simple integers or strings to complex JSON document. All these data are accessed in the database by using keys.
- Wide column stores (e.g. HBase, Cassandra). Much more similar to classics SQL databases, where data is stored in columns instead of rows. A facultative number of columns (also containing different types of data) can be grouped or aggregated as needed for queries or data views.
- Graph databases (e.g. Neo4j). Data is represented as a network or graph of entities and their relationships, with each node in the graph as a free-form chunk of data.
This flexibility means that we will be able to create documents without defining their structure first, add fields on the go, and use different documents with completely different structures. Also notice, with NoSQL databases, there are no relationships! In fact, we will use two mechanisms called Embedding and Referencing to join pieces of information.
Advantages of NoSQL Databases
Following there are some of the benefits of using NoSQL Databases:
- High speed for simple queries
- Highly scalable
- Extremely flexible structure
- No need for a database administrator. Since it is fairly user-friendly in this way, it can be used by both developers and administrators.
Field of use for NoSQL Databases
If you have problems establishing a specific schema for your database, your information structure is not clearly defined, or if simply your database schema will continue to change then go with a NoSQL database.
Applications that might need this solution are mobile-apps, content analyzing apps, or simply products from a rapid growth business.
Introduction to MongoDB
Now that we have talked properly about the differences between SQL and NoSQL solutions for our applications, it’s time to dive a little bit inside what we are going to use: MongoDB.
Let’s start with a definition: MongoDB is an open-source document database that provides high performance, high availability, and automatic scaling.
As a NoSQL database, MongoDB will have all the characteristics that we have mentioned earlier. And in particular, MongoDB is a document-based, which means that the data will be in a JSON-like structure that is flexible and easy to work with. Following, you can see an example of what the normal MongoDB syntax looks like :
As you may have noticed earlier, we have mentioned quite a few NoSQL database solutions for our applications. So why are we exactly picking up MongoDB between them?
There are many reasons for a person to be willing to try a new technology, but to be willing to learn something new I want to make sure to give you a nice reassurance that there is traction in the field, and that this is a great moment to learn this technology. So here are some of the reasons why you may want to learn MongoDB :
- Is a mature technology: after a rough start, MongoDB is now going strong and it has matured a very supportive community thanks both to code contributors, community testers and those who vote on new features.
- It’s a trending technology: If you don’t believe me, try to give a check to websites like this that will give you a little insight on the increasing popularity of this technology. Learning MongoDB now can mean to be step ahead of other developers, but most importantly to be an up-to-date professional in 2018. This skill might next lead you to a new, better career, salary improvement, or simply to be a more prepared developer.
- It’s free and open source: As if the popularity of the technology wasn’t enough to convince you, you will be able to learn it completely for free and even contribute to it!
- It’s easy to use and install: No need to suffer from long installation processes or staring at the command line for hours, MongoDB will be ready to for use in minutes!
And if that wasn’t enough, MongoDB doesn’t require you to be a master to start using it, in fact just be sure you have these things in your portfolio and you should be ready to jump in :
- Basic JSON syntax (this will make your life easier when starting to work with BSON). Read our tutorial on JSON here.
In particular, I would recommend you to give a refresh to Promises in ES6, since we are going to use them a lot.
Ready to start?
In the next section we will set up our environment for both mac and windows users, and we will make sure to have everything we need under the hood to make an amazing job, including MongooseJS and Mocha, which are tools that will help us to respectively interface ourselves with MongoDB and test our queries.
See you soon!