Why Comparing Postgres and MongoDB
The 2023 Stack Overflow survey shows that Postgres has become the most admired, desired database.
ICYDK, MongoDB used to hold that title for 4 consecutive years from 2017 to 2020. And according to DB-Engines, Postgres and MongoDB are among the top 5 databases. They are the two climbling the ladder and eating the shares of the big three, Oracle, MySQL, and Microsoft SQL Server.
MongoDB is widely perceived as a NoSQL database which is opposite to the SQL database clan including Postgres. But in recent years, they are converging:
- MongoDB becomes more like a traditional RDBMS, adding multi-document ACID transaction, secondary-index, advanced query capabilities.
- Postgres keeps improving its JSON capabilities such as indexing, query optimization, and more operators, which makes people wonder whether MongoDB becomes obsolete.
In 2018, folks at The Guardian wrote a lengthy post about migrating MongoDB to Postgres. A lot of things have changed since then, but one thing still holds true, it's always painful to migrate databases.
At Bytebase, we work with both databases extensively since the Bytebase product needs to integrate with both. Our founders also build Google Cloud SQL, one of the largest hosted database services.
Based on our operating experience, below we give an extensive comparison between Postgres and MongoDB from the following dimensions:
Unless otherwise specified, the comparison below is between the latest major release, Postgres 15 vs. MongoDB 6. We also use Postgres instead of PostgreSQL throughout the article, though we know the latter is the official name, which is considered as the biggest mistake in Postgres History.
License
-
Postgres is released under the PostgreSQL license which is a liberal Open Source license similar to the BSD or MIT licenses.
-
MongoDB community edition is released under Server Side Public License (SSPL), a license created by MongoDB Inc. itself to prevent others from offering the competing MongoDB service. MongoDB Inc. also provides commercial licenses upon request.
Most companies use databases to support their internal infrastructure, both Postgres and MongoDB permit this usage.
Data Model
Postgres is a relational database management system. It stores data in tables with predefined columns and data types. Relationships between tables are established using foreign keys.
MongoDB is a document-oriented database, which means that data is stored as documents in a collection. Each document is a JSON-like structure that can contain nested fields and arrays. MongoDB is designed to handle unstructured and semi-structured data.
Postgres (Tabular Model) | MongoDB (Document Model) |
---|---|
Database | Database |
Schema | N/A |
Table | Collection |
Row | Document |
Column | Field |
Postgres also supports JSON column, so one can also use Postgres in the MongoDB way, defining table as:
JSON Support
Both MongoDB and Postgres are very capable of handling JSON. MongoDB stores JSON using its own invented BSON, while Postgres uses a different JSONB format. For those interested, there is a lengthy discussion around whether to choose BSON or JSONB in Postgres.
MongoDB has 2 advantages:
- Built-in schema validator.
- Its integration with the Node.js/frontend ecosystem. MongoDB is a favored choice among full-stack developers who commonly utilize Node.
Performance
Performance is primarily determined by the access pattern. If an operation involves different entities, MongoDB is usually faster because data is de-normalized and doesn't require costly joins between tables. On the other hand, Postgres is more capable of handling complex queries thanks to SQL and its sophisticated query optimizer.
Reliability
MongoDB was known to be less reliable because it didn't support ACID transaction semantics in the early days. This has changed since they acquired WiredTiger and use its WiredTiger storage engine. Today, from the transaction perspective, MongoDB is as solid as Postgres.
MongoDB provides built-in automatic failover via its replica set. Postgres needs 3rd party solutions like pg_auto_failover.
Scalability
MongoDB scales out, while Postgres scales up.
MongoDB is a distributed database supporting automatic sharding. For Postgres, people usually scale up the single node postgres first and defer the sharding solution as late as possible. Of course, sharding Postgres is doable:
Usability
MongoDB doesn't enforce schema upfront and has an easy learning curve. Back in 2010, 10gen (the original name for the company that developed MongoDB) released the first version of the official MongoDB Node.js driver, which allowed developers to easily interact with MongoDB databases from Node.js applications. The driver provided a simple and intuitive API that supported a wide range of MongoDB features, including querying, indexing, and aggregation. Today, MongoDB still is the de-facto choice for full-stack developers because of its ease of use.
Postgres, as a relational database, enforces schemas. And even among the relational database group, Postgres is more rigorous than other peers like MySQL.
As the Stack Overflow survey shows, though Postgres is the most popular database among all respondents, MongoDB is more welcomed by the new learners.
On the other hand, MQL (MongoDB Query Language) is less powerful than SQL in handling complex queries.
Operability
Running a multi-node MongoDB is easier than running a multi-node Postgres, since sharding, failover are already handled by MongoDB itself. On the other hand, if you run both databases on a single node, then those distributed features in MongoDB become an overhead.
A variety of 3rd party vendors provide Postgres hosting service. MongoDB's license has prevented 3rd party from providing the MongoDB hosting service, and their own MongoDB Altas is more polished than the equivalent Postgres hosting options. After all, it's hard for 3rd party to beat 1st party cloud service.
Ecosystem
Postgres has an extensible architecture and is still maintained by the community. The Postgres ecosystem is thriving in recent years, it has a plethora of extensions making it more capable of handling different workloads than other databases. And thanks to its liberal license and solid architecture, for every application platform offering a hosted database service, they all choose Postgres. From the Heroku in the early days to the new Supabase, render, Fly.io.
MongoDB, as the most successful commercial open source business by market cap, takes a more commercialized approach. Whenever there emerges a good solution in the ecosystem, MongoDB Inc. would consider acquiring it to make sure it becomes an integral part of the overall MongoDB product line. Notable acquisitions include:
- WiredTiger for the storage engine.
- mLab for the hosting service.
- Realm for the mobile offering.
- Compass for the MongoDB GUI client.
Though Postgres and MongoDB take polarized approach to cultivate the ecosystem, both are widely successful.
Postgres or MongoDB
Postgres | MongoDB | |
---|---|---|
License | Postgres License (MIT alike) | SSPL |
Data Model | Tabular, relational | Document |
JSON Support | Capable and integrated with SQL | Built-in schema validator |
Performance | Optimized for complex query | Optimized for de-normalized data |
Reliability | Full ACID transaction support | Transaction + built-in auto failover |
Scalability | Scale-up | Scale-out |
Usability | Rigorous and powerful query capability | Relaxed enforcement and easy to start |
Operability | Optimized for single node and a wide range of hosting providers | Optimized for multi-node and a polished hosting service |
Ecosystem | Community driven, decentralized, vibrant | Business driven, centralized, integral |
There are obvious domains where one database is more suitable than the other:
- Choose Postgres if your application has complex business logic and requires complex queries.
- Choose MongoDB if your application has a simple data model and handles web scale data.
Yet, the majority of application use cases fall between them and both databases are very capable:
- MongoDB as a document database, naturally provides 1st class JSON support. While Postgres also has the best JSON support among all open source relational databases.
- For full-stack developers, MongoDB used to be the go-to choice because of its excellent Node.js integration. While Postgres has been attracting them as every major 3rd party application platform offers managed Postgres service.
- For backend developers, MongoDB becomes appealing ever since it added transaction support. Who doesn't want to avoid stressful schema change procedure and enjoy faster iteration cycle?
- MongoDB Atlas has built one of the most polished hosted database services. While many 3rd party vendors, from large to small, all provide great Postgres hosting solutions.
Overall, Postgres is a more versatile database. It adopts relational model, provides comprehensive SQL capability, carries an extensible architecture, and is driven by an enthusiastic community.
MongoDB is a holistic database solution. It popularizes document model, provides built-in scaling and high availability, offers an integral developer experience, and is driven by a sharp-minded for-profit business entity.
If choosing between Postgres and MySQL is hard, then choosing between Postgres and MongoDB is no easier. And as both databases are heading upward, the choice will only become harder 🤷♂️.
It's also common that Postgres and MongoDB co-exist inside an organization. And if you want to manage the database development lifecycle for both of them, please check out Bytebase.
Further Readings
- Bye bye Mongo, Hello Postgres
- Ask HN: Is MongoDB obsolote when Postgres and SQLite provide JSON types?
- Proposal: Add JSON support for Postgres
- Discussion for using BSON or JSONB for Postgres
- Sharding & IDs at Instagram
- Herding elephants: Lessons learned from sharding Postgres at Notion
- How to Handle Database Migration / Schema Change