DEV Community

Marc West
Marc West

Posted on

mongoDB or SQL?

MongoDB is a popular cross-platform NoSQL document-based database that was released in 2010. Maybe you are wondering if mongoDB is the right database for your current project, and the answer to that is simple: it depends.

MongoDB is great at doing some things and not so great at doing others. Deciding whether you should use mongoDB or an SQL database is something you should do early in the planning stages of a project. Choosing the wrong option can have damning ramifications for your project and produce catastrophic results. So, when should you use mongoDB?

Alt Text

MongoDB's strengths lie in its versatility. Unlike SQL databases, with mongoDB data is stored in collections of JSON-like BSON objects called documents. This means that it allows you to create and use databases without having to map-out and design a rigid schema. MongoDB is a great choice if you have heterogeneous data because the document system allows for adding unique information to a single document. Additionally mongoDB can store complex datatypes within a document allowing for nested objects and arrays. However, versatility isn't mongoDB's only strength.

If Node is your forte, mongoDB may be easy for you to pick up. It is frequently used in Node applications because of its familiarity and compatibility with JavaScript. Plus, the native query language for mongoDB is designed to emulate JavaScript. This means if you know JavaScript, then you already know how to query a mongoDB database. MongoDB also offers a cloud database, mongoDB Atlas, to make managing your data even easier. But for all its positives, mongoDB still leaves room for SQL in modern applications.

Let's take a look at how you might store some data in mongoDB vs an SQL database.

SQL

A fairly simple SQL schema

mongoDB

{
  "_id": "5cf0029caff5056591b0ce7d",
  "firstname": "Grap",
  "lastname": "Pina",
  "contact": {
    "email": grap@banas.com",
    "phone1": "5045554727",
    "phone2": "5045557462",
  },
  “dept": ["mgmt", "sales"]
}

Now we'll check out some example queries.

SQL

INSERT INTO users (username, password, email) VALUES (Grap, mamagrapmaidnname, grap@banas.com)

mongoDB

db.users.insert({
    username: 'Grap'
    password: 'mamagrapmaidnname',
    email: 'grap@banas.com'
})

While mongoDB can technically handle foreign keys, SQL is still king when it comes to highly-correlated data. If your database could be expressed well in a giant Excel spreadsheet, you should consider SQL over mongoDB. The atomic nature of data in an SQL database, that is - each cell of data contains exactly one item (a string, a number, etc), indexing data in SQL databases allows for fine-grain sorting that can't be achieved with complex datatypes in the mix. Lastly, in a broad sense, SQL queries tend to be slower on small data sets, but when your records number in the millions, SQL will blow mongoDB out of the water.

So where does that leave us? Well, although it isn't a total replacement for relational databases, mongoDB may be a better option for some projects. Consider using mongoDB if you need a more flexible, schema-less solution for working with unstructured data, but if you need to perform multi-row transactions on highly-structured data sets, you're probably better off sticking to SQL. Figure out which one is best for you before committing.

Top comments (0)