CodePark/ questions / SQL vs NoSQL & MongoDB
START PRACTICINGPRACTICE
Backend question · Databases & SQL › SQL vs NoSQL & MongoDB

When is a PostgreSQL jsonb column a good alternative to adding MongoDB to your stack, and what are its limits?

jsonb is a good choice when only part of a mostly relational model is semi-structured. Examples: product attributes, user settings, webhook payloads.

jsonb stores JSON in a parsed binary form. So you can query inside it, index it and combine it with normal columns:

  • A GIN index supports containment queries (@>, "does this JSON contain this value?") and key existence queries.
  • An expression index can target one path, like (attributes->>'sku').
  • You keep joins, transactions and constraints on the rest of the row.

The limits:

  • There is no schema enforcement unless you add CHECK constraints.
  • The query planner has weaker statistics for values inside JSON. So its row estimates can be poor, and it may pick a bad plan.
  • Updating one key writes a new version of the whole row, because of MVCC (PostgreSQL keeps old row versions instead of changing rows in place). This hurts with large documents that change often.

If the whole model is document-shaped, or you need built-in horizontal sharding (splitting data across many machines), a document database may fit better.

  • jsonb stores JSON in a binary form that you can index and query.
  • A GIN index supports @> and key existence queries, and expression indexes target one path.
  • You keep joins, transactions and constraints on the rest of the row.
  • jsonb has no schema by default, and the planner has weaker statistics for it.
  • Updating one key writes a new version of the whole row.