Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What factors would make you choose a relational database over a document database for a new service?

I would choose a relational database when the data has many relationships, when the service needs ad hoc queries and joins, and when it needs strong consistency with transactions across several records and constraints such as unique keys and foreign keys, which relational databases have refined for decades. A document database fits when data forms aggregates that are read and written as a whole and when parts of the schema genuinely vary. Team familiarity and operational support weigh in too, and since relational databases now offer JSON columns for the flexible parts, they are often the safer default.

Why this question is asked

Choosing the database is one of the few decisions in a new service that is truly expensive to reverse. Once data, queries and code assume a model, moving is a migration project. Interviewers ask this concept question to see whether we choose by the shape of the data and the queries rather than by fashion, and whether we know what each side does well. In this answer, we will recap the two models, go through the factors that favour a relational database, see where a document database fits better, add the human factors, and look at how JSON columns blur the line.

1A Quick Recap of the Two Models

A relational database such as PostgreSQL or MySQL stores data in tables of rows and columns with a fixed schema. Related data lives in separate tables and is connected by keys, and SQL joins combine it at query time. A document database such as MongoDB stores self-contained documents, JSON-like objects that can nest objects and arrays, grouped in collections. Related data is usually embedded in one document, so a single read returns everything for one entity.

Neither model is better in general. The relational model favours flexibility in how data is queried; the document model favours speed and simplicity in how one entity is read and written.

2Relationships, Ad Hoc Queries and Joins

When entities refer to each other a lot, such as customers, orders, products, invoices and payments, the relational model shines. Each fact is stored once, and any question that combines them can be answered with a join, including questions nobody thought of when the schema was designed.

SQL
-- a question nobody planned for: revenue per product category last month for repeat customers
SELECT p.category, SUM(l.quantity * l.unit_price) AS revenue
FROM order_line l
JOIN orders o ON o.id = l.order_id
JOIN product p ON p.id = l.product_id
WHERE o.created_at >= date '2026-10-01' AND o.created_at < date '2026-11-01'
AND o.customer_id IN (SELECT customer_id FROM orders GROUP BY customer_id HAVING COUNT(*) > 1)
GROUP BY p.category; -- answered with the existing schema, plus maybe one index

In a document database the same question needs an aggregation pipeline with $lookup stages across collections, or a model that copied the data in advance for this query. That is fine for questions we know about; for the stream of new ones that analysts, support and product will ask over the years, the relational model adapts with an index rather than a redesign.

3Transactions and Constraints

Relational databases have offered transactions across many rows and tables for decades, with well-understood isolation levels, and they enforce constraints inside the database itself: a unique constraint guarantees no two customers share an email, a foreign key guarantees an order line cannot point at a missing order, and a check constraint keeps a quantity positive. These rules hold no matter which service, script or person writes the data.

Document databases have added multi-document transactions, MongoDB since version 4.0, but they are designed around single-document atomicity, transactions carry more cost and limits, and cross-document constraints such as foreign keys do not exist; the application must enforce them. When correctness across records matters, as with money, stock or bookings, the relational side is the mature choice.

How the factors point
FactorRelationalDocument
Many relationships, joinsstrongweak, $lookup or copies
New ad hoc queriesan index awayoften a model change
Multi-record transactionsmature, cheapavailable, costlier
Unique and foreign keysenforced by the databaseunique only, no foreign keys
Aggregate read and written wholejoins or JSON columnone document, very natural
Varying fields per recordJSON columnnatural

4Where a Document Database Fits Better

A document database is the better fit when the data naturally forms aggregates, clusters of data always read and written as one unit, and other access is rare. A product catalogue where each product has a different set of attributes, a content management system storing pages with nested blocks, or user preferences that vary by feature are good examples. Reading one document returns everything, writes to one document are atomic, and adding a field needs no migration.

Flexibility deserves a careful look, though. A flexible schema is real when records genuinely differ in shape, such as a shoe with sizes and a laptop with CPU and memory. It is an illusion when the data actually has one shape that is merely not written down, because then the schema is enforced by the application code instead, and old documents in old shapes accumulate.

5Team Familiarity, Operations, and JSON Columns

Technical fit is not the whole decision. A team that knows PostgreSQL well will design, tune and debug it better than a database it is learning, and a company whose platform team already runs PostgreSQL with backups, monitoring and on-call support makes a new PostgreSQL database almost free to operate. Introducing a new technology means someone must learn to run it at three in the morning.

Modern relational databases also narrow the gap. PostgreSQL's jsonb type stores JSON documents in a column, indexable with GIN indexes and queryable with operators, and MySQL has a JSON type as well. That lets us keep the stable core relational and put the truly flexible parts in JSON.

SQL
CREATE TABLE product (
id bigint PRIMARY KEY,
sku text UNIQUE NOT NULL,
price numeric(10,2) NOT NULL,
attributes jsonb NOT NULL DEFAULT '{}' -- flexible part: varies per category
);
CREATE INDEX product_attr ON product USING gin (attributes);
SELECT sku FROM product WHERE attributes @> '{"color": "red", "size": "M"}'; -- uses the GIN index

With this, many services that would once have chosen a document database for one flexible part can stay relational and keep transactions, constraints and joins for everything else.

6Follow-up questions

Does a document database scale better than a relational one?

Document databases such as MongoDB offer built-in sharding, which helps when data or write volume outgrows one machine. But most services never reach the limit of a single well-sized relational server with read replicas, and distributed SQL databases now offer horizontal scaling with SQL. Scale alone is rarely the deciding factor for a new service.

When would you choose MongoDB for a new service without hesitation?

When the core data is an aggregate read and written as a whole, the shapes genuinely vary, cross-entity queries are rare or handled elsewhere, and the team knows how to run it. A content or catalogue service often fits that description.

Is jsonb a full replacement for a document database?

Not entirely. Large, deeply nested documents with frequent partial updates are more natural in a document database, and jsonb updates rewrite the whole value. For flexible attributes alongside relational data, though, jsonb is usually enough.

7Conclusion

In this answer, we saw that a relational database is the stronger choice when data has many relationships, needs ad hoc queries and joins, and requires transactions across records and constraints such as unique and foreign keys, all of which are mature. A document database fits aggregates read and written as a whole and genuinely varying shapes. Team familiarity and existing operational support also count, and JSON columns such as PostgreSQL's jsonb let a relational database hold the flexible parts, which often makes it the safer default.

What to say in the interviewAbout 90 seconds aloud

Open with

I look at the shape of the data and the queries. If the entities are connected, customers, orders, products, payments, and people will keep asking new questions across them, relational wins, because any new question is a join and maybe an index, not a model redesign.

Then, if they want more

Second, consistency: if I need transactions across several records and constraints like unique email or a foreign key from an order line to its order, relational databases have done that for decades and enforce it no matter who writes the data. Document databases now have multi-document transactions, but they are built around single-document atomicity and have no foreign keys.

A document database is the better fit when the data is an aggregate that is read and written as a whole, like a product page or a CMS page with nested blocks, and when the shapes genuinely vary. I'm careful there, because often the schema isn't really flexible, it's just not written down.

Then the human side: what does the team know, and what does the platform already run with backups and on-call? A database nobody can operate at 3 a.m. is a risk.

And finally, PostgreSQL's jsonb with a GIN index covers a lot of the flexible-schema cases, so I can keep a relational core with a JSON column for the varying attributes. That is why relational is usually my default unless the data clearly is document-shaped.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Data reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What factors would make you choose a relational database over a document database for a...": https://scrivio-interview-library.netlify.app/databases/spring-data-nosql/9646ffa9da/