MongoDB vs SQL Databases [Tech Debates]

You should already know that MongoDB is an open-source, NoSQL database that is gaining popularity each and every day, but why you should consider it in your next project and use it instead of a well-known, matured, relational and structured database like MySQL?

Data Structure
If you want to store large volumes of unstructured data, you should really consider a NoSQL database like MongoDB, otherwise you should deal with complex schemas, migrations and schema changes over time.

Performance
Comparison benchmarks show different results and it is dependent on the use case and how the data is stored and organized, but in most cases we can see that:

• Inserting Data is faster in MongoDB, because it does not have to ensure the data is consistent with the schema. Updating data is also faster, since MongoDB won’t perform foreign key checks.
• Accessing data by an attribute is faster in SQL databases, especially when searching for an attribute that is unindexed in MongoDB. Indexing fields in MongoDB can boost the speed dramatically. Accessing non keyed attributes are faster in SQL databases, but when the attributes are indexed as keys, MongoDB can sometimes outperform the rivals. You can have up to 64 keys in a MongoDB collection, so don’t forget to index your data.
• SQL databases are also faster in sorting.
• Aggregation of data is faster in MongoDB.
• If you used nested references in your MongoDB collection, fetching and updating data is a lot faster than joining the tables in a SQL database. You should not store your data in a MongoDB the same way as a SQL database, if you really want to see a higher performance.

MongoDB tries to use all the available RAM via the file system cache. It automatically uses all free memory that is not used by the cache engine or by other processes and this is coherent if you’d like to have maximum performances. So system resource monitors often show that MongoDB uses a lot of memory, but its usage is dynamic. If another process suddenly needs half the server’s RAM, MongoDB will yield cached memory to the other process.

As a consequence, the single parameter you can tune to optimize memory usage is the engine cache size.

Scalability
If your data is going to grow unbounded over time, you need some sort of database technology that is not limited to the data that you can store. MongoDB can distribute the storage of your data across an entire cluster and scale horizontally instead of vertically.

You should also consider your transaction rate. If you have thousands of transactions per second, one database system will not be enough for sure.
With MongoDB built-in replication and sharding capabilities, your data storage and transaction rate can grow over time very easily.

Software Stack and Rapid Development
MongoDB can be used from all major languages, but the solution or software stack that you are going to choose for your next app can greatly affect your database choice. If you want to choose Node.js as your backend for example, the perfect combination is a NoSQL database like MongoDB, wherein the schema need not be well-structured. MongoDB represents the data as a collection of documents rather than tables related by foreign keys. This makes it possible for the varied types of data to be stored decently and accessed in the web applications using Node.js.

That’s why the MERN or MEAN stacks are popular these days, letting you write JavaScript for the client, backend and database layer, with its schema-less nature is a better match to the constantly evolving data structures in web applications.

Document databases put developers in charge of the data. Data becomes like code that is friendly to developers. If you are developing using modern agile methodologies, a relational database will slow you down. A NoSQL database doesn’t require the level of preparation typically needed for relational databases.

SQL Attacks
With MongoDB there is a less risk of SQL injection attacks due to the design, but external injection is also possible in NoSQL database management systems such as MongoDB. In most cases, external injections happen as a result of an unsafe concatenation of strings when creating queries. So, you still need to validate your data.

Hosting
If you don’t have the time, budget or in-house expertise to face the challenges of managing a massive database yourself, there are a lot of managed MongoDB providers on the public clouds that you can choose from. You can setup your MongoDB cluster in a few minutes and add new nodes when you need to. There are even some free options if you are just in the development phase right now and the data size is less than 500mb.

You can even install and manage MongoDB on a free web control panels like CWP. More about CWP later on, but you need to make sure you have someone available who knows what they’re doing for setting this up in a secure manner.
If you are in a small organization, maybe it’s better to choose a paid option with professional support.

Use cases
MongoDB can be a better choice for social networks, real-time analytics and data integration, product catalog, internet of things, mobile apps and gaming.

Reasons why you might consider a SQL database are:
1. You need ACID compliance. Acid stands for (Atomicity, Consistency, Isolation and Durability). ACID compliancy reduces anomalies and protects the integrity of your database. It does this by defining exactly how transactions interact with the database, which is not the case with NoSQL databases, which have a primary goal of flexibility and speed, rather than 100% data integrity.
2. Your data is structured and unchanging: If your business is not growing exponentially, there may be no reason to use a system designed to support a variety of data types and high traffic volume.
3. You need stored procedures: In MongoDB, there is not any provision for Stored Procedure or functions or trigger, so there are no chances to implement any business logic in the database level which can be done in any RBMS systems.

Conclusion
Not every database fits every business need. That’s why many companies rely on both relational and non-relational databases for different tasks. Although NoSQL databases have gained popularity for their speed and scalability, there are still situations in which a highly structured SQL database might be preferable.

That’s it for now. Let us know your opinion in the comments. We will update the debates every once in a while.
If you like this video, don’t forget to subscribe on our YouTube channel for more tech debates like this one.

Image for post
Image for post

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store