When comparing MongoDB vs Postgres, it is important to choose the right product to store your most prized possession which in most organizations is data. First look at MongoDB, you will be impressed to know that the underlying data structure are documents. This document-centric data store uses JSON-like documents with schema. Postgres on the other hand is an object-relational database. If you’re familiar with stored procedures, triggers, views and materialized views then you will feel right at home with Postgres.
Let us peel off the covers and see which of these two products will be the right choice for your application. Both are great products but depending on your needs, one of them will win your business while the other one will be close behind.
As far as differences are concerned, this is how they differ:
- MongoDB is a document-oriented database vs Postgres is an object-relational database.
- MongoDB was developed by the MongoDB Inc. vs Postgres is the product of the PostgreSQL Global Development Group.
- MongoDB is extensible using plug-ins vs Postgres is highly extensible.
Replication is the process of synchronizing data across multiple servers. Why would you want the same data replicated or occurring on multiple servers? The reason is to have data redundancy so that if one server goes down, you can be assured that the same data exists on another server. Remember data redundancy increases data availability. Also having the same data on multiple servers help facilitate backup and maintenance.
MongoDB achieves replication by implementing ReplicaSets. A replica set is a group of mongod processes that maintain the same data set. In a replica, one node is the primary node and that node receives all the write operations. All other instances or secondaries apply operations from the primary so that it stays in sync with the primary data set. If the primary node goes down, a new primary node is elected. After recovery of the failed node, it joins the replica as a secondary.
Postgres replication is based on a mature and long used technology called ‘Write Ahead Log (WAL) Archiving’. With the latest version of Postgres, there has been some significant enhancements producing extremely fast WAL processing that results in near real-time replication.
All objects, data (including the schema) and security operations executed on the master are written to the WAL log directly on the slave machine for safety. WAL also ensures that no transaction is committed on the master until a successful write of the WAL log has occurred. The slave then applies the WAL log by directly rewriting the raw table data on disk which is a lot faster than statement based replication used in other database servers such as MySQL.
Postgres is magically faster than MongoDB if documents are stored in a tabular format but that’s not the case with MongoDB because documents are stored in JSON format. When data in Postgres is stored as JSON documents, it no longer is faster than MongoDB. The morale of the story is that we cannot compare MongoDB with Postgres because they are radically different.
PostgreSQL is widely used in large systems where read and write speeds are crucial and data needs to be validated. In addition, it supports a variety of performance optimizations that are available only in commercial solutions such as Geospatial data support, concurrency without read locks, and more.
Overall, PostgreSQL performance is utilized best in systems requiring execution of complex queries often found in data warehousing applications and business intelligence analysis and reporting. PostgreSQL performs well in OLTP/OLAP systems when read/write speeds are required and extensive data analysis is needed.
PostgreSQL also works well with Business Intelligence applications but is better suited for Data Warehousing and data analysis applications that require fast read/write speeds.
MongoDB excels in web applications that has humongous document data stores such as registries, insurance or any industry that must keep documents for numerous years. It is not good in applications where analytics are performed or where joins are required because there is no joins in MongoDB. Instead of relating two documents like you would in a relational database, you would have to embed one document in another.
SQL is usually the common language to query a relational database and since Postgres is relational, the query language it uses is known as PostQuel which is a variant of the SQL standard. It is pretty standard from a purist point of view because out of the 179 mandatory features required for full Core conformance, Postgres satisfies about 160 features.
Since MongoDB is a NoSQL database, one cannot expect to access data using SQL. Instead, MongoDB uses the db.collection.find function to search and retrieve documents in collections. To speed up retrieval, MongoDB uses indexes to help cut down retrieval times. Every time a document is stored in MongoDB, it is assigned an object id which is a unique identifier. You can either have MongoDB assign the object id or you can define your own by specifying an ‘_id’ field. The id field is defined as the primary key of a document in the collection. Either way, MongoDB will utilize the id field to find the necessary documents.
Both MongoDB and Postgres have cursors which allow the user to step through a collection. In the case of MongoDB, after using the db.collection.find function, the result set is a collection of documents. Each document has a set of fields that can be manipulated or updated. Postgres also has the ability to step through each record in a result set, accessing columns in each record.
Another similarity between MongoDB and Postgres is that both share the limit and order clauses. The limit clause allows you to set an upper limit to the number of items returned from a search. The order clause allows you to order or sort the result set either in ascending or descending order.
Indexes enhance database performance as they aid the database server in locating and retrieving rows must faster than without an index. But unfortunately, indexes add a certain overhead when it comes to inserts and updates. Without an index, the database server must start with row one and scan the entire table, one row at a time to look for a specific condition. This is known as a linear search and linear searches are the slowest among all searches.
MongoDB offers six types of indexes. They are single field index, multikey index, compound index, geospatial, text and hashed indexes.
Postgres offers three types of indexes. They are partial indexes, bitmap indexes and expression indexes. Even with additional index types, Postgres is still slower than MySQL when it comes to retrieval.
There are two options to install MongoDB. One way is to install it on a Windows machine and the other is to install it on the cloud. At the time of writing, there are three managed service providers. These include:
- MongDB Atlas
- Amazon DocumentDB
- Costmos DB API
MongoDB Atlas is the global cloud database service that allows fast deployment using a variety of cloud-based providers including AWS, Azure and GCP. Amazon DocumentDB only offers AWS and Cosmos only offers Azure.
A trending topic these days is deploying Postgres using Kubernetes. But what is that, you may ask? Kubernetes is an open source container orchestration system for automating deployment of Postgres. It uses a stateful container using persistent volumes, statefulsets and more. Why is this important? Data generated inside the container is usually lost after termination of the container instance. With persistent volumes, data can be saved and can persist after the deployment completes. This is important for initialization routines or setting of configuration parameters.
Based in the United Kingdom, this company nets $10M and uses MongoDB to store and track resources’ resumes, recommendations and reference letters.
Skype uses Postgres to store all Skype chat and call activities. Calls are routed through an external VOIP service which also uses Postgres.Skype chose Postgres because it wanted to scale its business to over a billion users.
Fashion Nova is the top online fashion store for women and men. This company uses MongoDB to store and manage its inventory of dresses, pants, suits, outfits, blouses and accessories.
CarFax uses MongoDB to power their business and to manage 11 billion records. It allows a download of up to 3.8 billion documents yearly and manages 50,000 online education registrations and more.
eBay uses MongoDB to conduct a number of tasks that involve a huge amount of data such as cloud management, search suggestions, metadata storage and merchandise categorization.
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, I only recommend products or services I use personally and believe will add value to my readers.