Skip to main content

MongoDB or SQL Databases: Which One Should You Use?

Data Product Executive
Published on October 2, 2024

Analytics on MongoDB is a completely different beast from typical business intelligence setups that you're familiar with. Don't expect to just fire up your existing BI tool, click at MongoDB, and let it run. It's far more complicated than that. This article explains why MongoDB has essentially a different approach, which is necessary to understand before exploring your options.

MongoDB or SQL Databases: Which One Should You Use?

MongoDB or SQL Databases: Which One Should You Use?

Analytics on MongoDB is a completely different beast from typical business intelligence setups that you're familiar with. Don't expect to just fire up your existing BI tool, click at MongoDB, and let it run. It's far more complicated than that. This article explains why MongoDB has essentially a different approach, which is necessary to understand before exploring your options.

Firstly, a general overview of a relational and non-relational database!

MongoDB's approach is focused on integrating the important capabilities of relational databases with the innovations of non-relational databases like noSQL. Their key concept is to combine capabilities of relational database tools like Oracle, MySQL and others, with requirements of modern applications.

A relational database serves the purpose of structuring data, such as transactions, in tabular form, and offers features that remain critical today for businesses.

So what are key differences between relational vs non-relational databases?​

Relational databases offer features that allow us to query, analyze, dissect and display structured charts of data found in these SQL databases. Relational databases enable you to:

  • Utilise Structured Query Language (SQL) & Database Indexes - Users are able to access and maneuver their data in mature ways to assist both operational and analytical applications. The concept of a relational database enables you to connect information from different tables through the use of database indexes, which is essential for applications that are heavy into data analysis.
  • In a relational database, data is stored in the form of tables and rows. Each row contains information about specific entries and each column contains a very specific type of data.
  • A well-designed schema decreases data repetition and prevents tables from becoming messy. This is an important feature for many businesses, especially those who store large amounts of financial data.

However, with MongoDB, the value of a NoSQL database is mainly its simplified design, horizontal scaling and control capabilities over the availability of data.

  • Adaptable Data Mode - A NoSQL system supports any form of data structure. Either document, graph, key-value or wide-column, all of them offer a flexible data model, which makes it easy to store and incorporate data of any structure and allow vital adjustments of the schema without impacting performance.
  • MongoDB data is stored in various hosts of databases. MongoDB data is stored in a collection of JSON documents. The Mongo import feature can import JSON, CSV and TSV file formats.
  • Scalability and performance - This important feature is an important differentiator between the two types of databases. With a NoSQL database; it has been built to scale, they all include sharding - a method for distributing data across multiple datasets, and partitioning - breaking down data into chunks. This allows the database to scale, having theoretically unlimited growth with the maximum rate of production and lower inactivity than a relational database.
  • Speed - As MongoDB is a document-oriented database, it is easy to access your documents by indexing. Hence, this provides a faster query response. Reading a record whose related data is embedded in a single document avoids the joins an equivalent relational query would need — though how much that wins by depends entirely on your data model, indexes and access patterns, so treat any headline speed multiplier with suspicion.
  • Ad-hoc querying - MongoDB is very flexible and has advanced ad hoc query features.
  • Easy setup - MongoDB is said to be easier to set-up than a relational database management system (RDBMS).

So what differentiates relational vs non-relational databases for analytic purposes?​

MongoDB wasn't initially created to support advanced analytics. But as time has progressed and the requests for real-time data from the database grows, this has driven MongoDB to build more analytic capabilities directly in the NoSQL database.

However, when enforcing Operational BI solutions, many users usually think of duplicating the operational data to an operational data store (ODS), either a data warehouse or data mart and analysing it there. This will immediately cause problems of how to update the information stored, fast enough to support the requirements of the users. With true real-time data streaming approaches, traditional batch extract, transform and load (ETL) tools will not be suitable. Furthermore, the ability for typical data warehouses to provide real-time updates and modifications of existing data is questionable.

Nevertheless, there are always advantages and disadvantages. Avoiding the need to store and maintain an alternative copy of a large body of data is generally good. If the analysis does not require joining data from another source, using the original source can also be helpful.

There will always be questions about performance impacts on the operational sources, and often security concerns as well. However, when comparing the types of queries possible for a NoSQL store or a document-oriented database, it is commonly acknowledged that normalizing data in a relational database provides a more query-friendly structure, which allows a wider variety of queries to be controlled. In the case of using an operational BI tool, queries are usually much simpler.

MongoDB supports several analytic capabilities.

Its main in-database analytics tool is the aggregation pipeline, which chains stages to filter, group, reshape and join data — this is where $lookup lives. It also supports Apache Spark, the popular data science framework that is familiar with data scientists, engineers and data analysts.

mongodb-spark

MongoDB also offers a SQL interface that lets users explore their MongoDB data through different business intelligence tools like Holistics, Looker, Microsoft Power BI and more. Note that the older BI Connector is end-of-life and unsupported after September 2026; MongoDB now points new projects at the MongoDB SQL Interface instead.

Common issues with MongoDB​

Every company has a different set of data requirements and environments. An effective data infrastructure is built to meet business challenges, and to ease the process of data reporting. Depending on your organization's needs, MongoDB may or may not be the best solution over a relational database.

Here are few things to consider when you are weighing your options:

  • Joins work differently - MongoDB does support joins, but not in the way a relational database does. The $lookup aggregation stage performs a left outer join against another collection in the same database, and since MongoDB 5.1 it also works with sharded collections. What you don't get is the relational engine's freedom to join anything to anything: $lookup is a pipeline stage you assemble deliberately, it has no direct equivalent to a right or full outer join, and joining large collections can be expensive. In practice, MongoDB expects you to model related data together in a document rather than to normalize it and join at read time.
  • Different skill-sets and tools required - There are few question that you need to answer when you are considering using MongoDB. How invested are you in your current relational database? Is your team familiar with SQL? If you are a smaller company with less resources to invest, MongoDB is one of the easier NoSQL databases to set up and manage.
  • Data location - When working with MongoDB, you'll need to be aware of a few things. Firstly, where is your data coming from, or will you be pulling data from certain locations? MongoDB's powerful query application supports this system well.
  • Data Size - Your BSON data or documentation size is limited to 16MB.
  • Memory and storage overhead - MongoDB stores the key names inside every document, so field naming has a real cost at scale. Denormalized document models also duplicate data by design — that's a deliberate trade of storage for read performance, not a consequence of missing joins.

Yes, these points above may demotivate you to consider moving into a MongoDB database. Hence, I have covered both the positive and negative points of the system. We have to understand the overall usability of the system.

How you handle relationships is the key difference between the two systems. It isn't that MongoDB can't join — it's that the document model encourages you to avoid needing to. If your data is fragmented across numerous servers, joins get expensive, and embedding related data in one document sidesteps that cost. MongoDB, like other NoSQL tools, plays well in the Web space with organisations processing large volumes of data with emergent, fast-changing requirements. However, there come many challenges with it as well. Because there are several different ways to relate document collections — embedded lists, embedded objects, or references resolved with $lookup — the right structure depends on the queries you expect, and a model tuned for one access pattern can be awkward for another. Ad-hoc questions are where this bites: SQL lets you express an unanticipated join over a normalized schema in one statement, whereas the equivalent in MongoDB may mean a multi-stage pipeline, or reshaping your documents.

What do you think?

Do share with us your thoughts and comments on MongoDB for analytics in the comments section below.

On this page