DEV Community

OM
OM

Posted on

The Query That Should Pick Your Database

The Query That Should Pick Your Database

Teams pick MongoDB or PostgreSQL by arguing about schemas. The schema debate is real, but it is not the decision. The decision is the query you will run most, at month twelve, when the data is no longer small and the person who chose the database has left.

Write that query down before you choose. Not the entity diagram, not the demo query, the actual production question: "all orders for this customer with their items and payment status, sorted by date, paginated," or "every device that reported in the last hour, grouped by fleet." The shape of that sentence tells you more than any comparison table.

When the query is relational, the document model taxes you forever

If the question joins across entities - customers to orders to payments, devices to fleets to regions - a relational database answers it in one round trip with decades of planner optimization behind it. A document store answers it by embedding copies (which then disagree), by $lookup stages that work but age badly, or by application-side joins you now maintain forever. None of these is wrong. All of them are a tax you pay on every release, forever, because of a decision made in week one.

When the document is the product, the relational model taxes you instead

The reverse is equally true. If your core object is a self-contained thing - a catalog listing with wildly varying attributes, a form definition, an event payload - then shredding it into twenty tables to satisfy a normal form is its own tax. Every read reassembles what was naturally one document. Every new attribute is a migration meeting.

The honest test is not "is our data structured?" All data is structured. The test is: does the most important read load one aggregate, or does it correlate many? One aggregate favors the document model. Correlation favors the relational one.

The boring option is usually right

PostgreSQL now stores and indexes JSON well enough that "we need flexibility" is rarely a reason to leave it. That means the default answer for a small team with a mixed workload is Postgres, and the burden of proof sits on the document side: you should be able to name the specific access pattern that justifies the second system, the second backup routine, and the second set of operational instincts.

A full side-by-side, including where each model's assumptions break under real workloads: https://omnistu.com/blog/mongodb-vs-postgresql/


This article was created with the help of AI.

Top comments (0)