DEV Community

Cover image for Understanding PostgreSQL and MongoDB Databases: Know When to Use Each
Peter Gitonga
Peter Gitonga

Posted on

Understanding PostgreSQL and MongoDB Databases: Know When to Use Each

When it comes to database management systems (DBMS), PostgreSQL and MongoDB will often find themselves in the spotlight. But choosing any of them depends on the specific requirements of your project. In this article we are going to explore them, understand what they are and where each applies best.

I have seen posts saying "Which is your go-to database?" then goes ahead and lists about four different databases. I don't have a problem with that if it's one's hobby project, or if they're trying to learn. But beyond this, you'll have to fetch from the toolbox the right tool for the job.

Before we proceed, let's understand the terms you might come across:
Data model - an informal description of a database
Schema - a blueprint or a formally implemented structure in use by the database e.g. in Postgres, names of tables, names of columns in a table and their corresponding data types.
Mapper - high-level package helping connect to databases and perform database operations

PostgreSQL

PostgreSQL or simply Postgres(because no one has time for syllables) is a relational database management system (RDBMS) that follows a structured and tabular (with rows and columns) data model.

Postgres enforces a predefined schema requiring well-defined structures of the tables that ensures data consistency and integrity.

It uses SQL (structured query language), a standardized language for managing relational databases, to command the server.

It's structure consists of records which are rows containing data and tables which are groups of rows and columns. Column names are used as keys when querying for records.

Some of the Postgres ORM's (Object-Relational Mapper) include Sequelize, pg.

Postgres Applications
Use Postgres:

  1. For demanding applications that require complex queries and relationships, advanced data types and indexing options.
  2. Where data integrity is critical i.e data is accurate, consistent and reliable while within the database. Postgres is ACID compliant.
  3. It has a mature ecosystem, guaranteeing support, and a wealth of resources.
  4. Where vertical scalability is more preferred - adding more resources to a single server. Horizontal scaling (distributing servers around the world) in Postgres requires more effort.
  5. Suitable for systems with fixed data models like financial systems, CRM systems.

MongoDB

MongoDB is a NoSQL database that follows a document-oriented data model and stores data in JSON-like BSON documents.

MongoDB allows for dynamic schema or schema-less design making it flexible and agile in accommodating unpredictable future data structure changes, and easy to scale horizontally.

For query language, it leverages on a number of query operators based on JSON-like documents, which works well with semi-structured and unstructured data. The language is also simple and ease to use.

It's structure comprises of documents as data, and collections as groups of related documents.

Some of the MongoDB ODM (Object-Document Mapper) include Mongoose

MongoDB Applications
Choose MongoDB:

  1. If the database requires flexibility in adapting to changing data structures without a predefined schema.
  2. Agility in development is paramount - dynamic/schema less nature of MongoDB makes it agile in development, especially for unpredictable data structures since you won't have to go back adjust a predefined schema for your changes to be acommodated.
  3. Simplicity and ease of use performing database operations since it uses BSON documents that use JSON-like language.
  4. Effortlessly scale horizontally to accomodate growing datasets and high write loads.
  5. Suitable for systems with unrestrictive data models like real-time big data, IOT and CMS systems.

In conclusion, the choice of the database to use relies on the requirements of your project. Use Postgres for data that requires a predefined schema to ensure data integrity and consistency. On the other hand, MongoDB is the best option for the data whose structure keeps changing over time since its dynamic/schema-less nature makes it flexible to accommodate such changes.

Top comments (2)

Collapse
 
kwnaidoo profile image
Kevin Naidoo • Edited

This is a good article. After much pain with MongoDB in production, I learned the hard way - it's too much of a headache to manage and query.

A good alternative to Mongo is "Scylladb" if one needs to go down the NOSQL root.

Scylladb gives you similar and usually better performance than MongoDB, but also is compatible with Cassandra drivers - so you get to query the DB using a similar language to SQL, thus making it easier to work with if you come from MySQL or PostgreSQL.

Collapse
 
pptrgi profile image
Peter Gitonga

Scylladb sounds like a better alternative, I'll try it. Thanks for sharing.