SQL vs MongoDB(noSQL)! How to decide between databases

Emrich-Michael Perrier
4 min readAug 23, 2021

Recently I’ve been tinkering with the MERN stack. I’ve heard this stack is pretty popular and wanted to get into NodeJS & ExpressJS because of that. Commonly webapps will use MongoDB when getting into node and express, but when is MongoDB useful? Is it better to use one compared to the other? What is a noSQL database? I hope to clarify and answer these questions in this post! Let’s dive in!

What is noSQL?

a NoSQL database is non tabular and stores data differently than relational table databases like classic SQL. NoSQL databses will store data in multiple ways including…

  • Key-value data stores
  • Document stores — store data in JSON, XML, and/or BSON documents, similar to key-value stores
  • Wide-column stores
  • Graph stores

NoSQL database store differently. Because of this people tend to think that you can’t do relationships with a NoSQL database, but that is not the case, it just stores these relationships in a different way. Many actually find setting model relationships is easier in NoSQL databases easier because related data doesn’t have to be split between tables. NoSQL relationships model data can be nested within a single data structure which makes grabbing that data faster compared to a classic relational SQL database. Additionally NoSQL reduces total storage demand for the same data which makes it good for saving on storage costs and provides data faster.

What is MongoDB?

MongoDB is a NoSQL database that stores information in JSON-like documents with optional schemas — instead of storing data into columns and rows like a traditional relational DB. It’s very scalable and is used by numerous modern webapps today. It’s also commonly used with ExpressJS and NodeJS as the middleware to handle the Database transactions.

What is SQL?

Just for good measure I’ll go over what SQL is. SQL is a classic relational database that’s been used for storing data since it’s release in 1979. It stores data in tables with rows and columns. It stores relational data in a separate join table that uses the id of one model to be declared as being related to another. It’s most commonly used for relationships between models.

SQL vs MongoDB — When to use

The real answer is that it depends what you’re building and even so there isn’t a clear cut answer to use one compared to the other in a given situation. At the end of the day they both store data, you’ll just get certain advantages for one compared to the other

NoSQL databases like MongoDB are going to be perfect when you need to store data that doesn’t have relation and you need to hold a lot of data. You’ll be able to pull data very quickly and pull tons of it. Additionally for storing data in the cloud, you won’t hit limitations so quickly compared to if you were to use SQL for cloud storage. Furthermore, if you need relationships you can still use NoSQL but it most likely won’t be your best bet if you need to make specific and complex queries to the database.

SQL is going to be your best bet if you don’t anticipate huge changes in data storage and rely heavily on model relationships. SQL is also going to be great if you need complex queries and reports because you’ll be able to find very specific data with JOIN queries.

An example of me using a NoSQL database recently would be the word API I made. I generated this API to self teach myself nodeJS/ExpressJS and tinker with mongoDB. It’s very simple but if I were to scale it up then I think it would be a great use of NoSQL. There are no relations and it would just be a huge database of words with their attributes. You can check that out here. Additionally I’m also using MongoDB to develop a fullstack app with a friend. This will need model relationships which is possible and still very efficient.

I hope you guys learned something from this! I am just getting started in learning technologies that I haven’t used at my time in the FlatIron School. In such a fast growing industry it’s important to learn the technologies that are emerging to make sure your product is the best that it can be. Additionally you can add tons of value to yourself as an engineer by learning new technologies like this one. That is what will separate a good engineer and a great engineer.

Here are some links that helped me understand the concepts of SQL vs NoSQL DBs…

Happy coding everyone!

--

--

Emrich-Michael Perrier

Fulltime Frontend Developer, Anime nerd and lifter of heavy weight in competition