Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for I Replaced MongoDB with a Single Postgres Table

I Replaced MongoDB with a Single Postgres Table

Shayan on June 18, 2025

Look, I need to show you something that might hurt your feelings:CREATE TABLE MongoDB ( _id UUID PRIMARY KEY, data JSONB); Enter f...
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
brense profile image
Rense Bakker
I am a fullstack developer with experience designing and building responsive web apps. Proficient with ReactJS, Typescript, Javascript, HTML and CSS.
  • Joined

Jsonb is well known, but so so slow compared to doing aggregations with mongodb. For simple use cases yes, you don't need that performance, but by simple I mean: your personal blog. In 12 years I have never encountered any enterprise app where jsonb was a good choice in the long run.

CollapseExpand
 
faeizmn profile image
Faiz Mansuri
  • Joined

I agree , I tried this with gin index to try if it really is faster than mongo.
it wasn't
Writes were extremly slow
Reads and updates were keeping up with thehelp of indexes.
Git link -github.com/faeiiz/Jsonb-Postgresql

CollapseExpand
 
faeizmn profile image
Faiz Mansuri
  • Joined

The only time i have used jsonb was when i was capturing update logs in a log table in postgress and storing data of the user had changed. 2 collumns -> old_data and new_data(updated value).

did'nt want to store the whole object.

CollapseExpand
 
brense profile image
Rense Bakker
I am a fullstack developer with experience designing and building responsive web apps. Proficient with ReactJS, Typescript, Javascript, HTML and CSS.
  • Joined

That sounds like a good use case

Thread Thread
 
faeizmn profile image
Faiz Mansuri
  • Joined

it is. I was developing a crm systems and wanted to have audit trail on individual profiles. Made a audit table storing data about who , on what (Vendor , Resource) , and what changes they did -> Used JSONB column for this. show logs to the admins.

CollapseExpand
 
lemii_ profile image
Lemmi
  • Joined

Thx this is first time I'm hearing about jsonb. very cool.

CollapseExpand
 
shayy profile image
Shayan
Building UserJot in Public
  • Location
    Maryland, United States
  • Joined

haha thank you :)

CollapseExpand
 
nevodavid profile image
Nevo David
Founder of Postiz, an open-source social media scheduling tool.Running Gitroom, the best place to learn how to grow open-source tools.
  • Education
    Didn't finish high school :(
  • Pronouns
    Nev/Nevo
  • Work
    OSS Chief @ Gitroom
  • Joined

pretty epic seeing someone call out most nosql hype like this - ever find yourself picking tech just to avoid migrations, or is it usually about performance?

CollapseExpand
 
shayy profile image
Shayan
Building UserJot in Public
  • Location
    Maryland, United States
  • Joined

Sometimes avoiding migrations is a nice side effect, but it’s mostly about getting solid performance for the use case.

CollapseExpand
 
dotallio profile image
Dotallio
  • Joined

I’ve used JSONB that way for tons of event logs and custom app configs - it honestly feels like cheating sometimes. What’s the most complex thing you ever modeled in JSONB?

CollapseExpand
 
shayy profile image
Shayan
Building UserJot in Public
  • Location
    Maryland, United States
  • Joined

Love that use case. I once modeled a dynamic form builder with nested rules and it handled it beautifully.

CollapseExpand
 
xwero profile image
david duymelinck
Learned to code in the wild west time of php 4, also the time xml and xpath where the new hot thing.
  • Location
    Belgium
  • Joined
• Edited on• Edited

While jsonb is great, just from the examples I see the overuse of the column type.

User Preferences/Settings: Every user wants different things. Don't create 50 boolean columns.

Create a preferences table with an id and the name of preference, and have a pivot table with the ids of preferences and user ids.

Product catalog and form submissions, a similar solution.

I see more a shortcut to not do data normalization, than splitting document structured data from relational data.

While they are pushing you to their Atlas solution, there is still the freecommunity server. No need to pay, just a willingness to set it up.

From the post you made i see you had a good experience with jsonb, and that is great. The problem I have that the information you provide is skewed by the experience. it is a trap I fell for many times, so I'm as fallible as you. Over time I learned all tools have their good and bad sides, and we have to use them for their good sides.

CollapseExpand
 
rafael_nacle_b831c11bd23c profile image
Rafael Nacle
  • Joined

That is new to me! Really cool stuff! thanks for sharing!

CollapseExpand
 
shayy profile image
Shayan
Building UserJot in Public
  • Location
    Maryland, United States
  • Joined

Sure thing!

CollapseExpand
 
kvetoslavnovak profile image
kvetoslavnovak
  • Joined

This is really great article. A gem in a pile of AI crap usually posted on DEV.to nowadays.

And I agree that JSONB brings the best of both worlds. Moved from MongodDB two years ago using exactly the same strategy. We also combine this with static JSON files stored in buckets storage.

The big plus is also full text search. This cannot be achieved in MongoDB but is easily done with JSONB.

CollapseExpand
 
franckpachot profile image
Franck Pachot
🥑 Developer Advocate at 🍃 MongoDB, 🔶 AWS Data Hero, 🐘 PostgreSQL fan,▝▞ YugabyteDB expert, 🅾️ Oracle Certified Master, and 💚 loving all databases 🛢️
  • Location
    Lausanne, Switzerland
  • Education
    Master MIAGE, Université Paris-Sud, France
  • Work
    Developer Advocate at MongoDB
  • Joined

This approach works only on very small databases because indexing this structure is more challenging and limited compared to document databases like MongoDB. Using UUIDv4 for IDs can be harmful (UUID PRIMARY KEY DEFAULT gen_random_uuid()), as these IDs are scattered throughout the table, which can negatively impact performance, especially with shared buffer caches and B-Trees.
But there is worse with the single JSONB. I've written a series on the pain points people experience when using PostgreSQL as a document database. The lack of Index Only Scan, the lack of range or sort covering with GIN index, the read and write amplification:dev.to/mongodb/no-hot-updates-on-j...

CollapseExpand
 
devmakasana profile image
Sanjay M.
Founder of Teamcamp
  • Joined

Brilliant deep dive! This perfectly showcases how versatile and powerful Postgres + JSONB can be for modern app needs. A great reminder that sometimes one optimised tool can replace a stack of complexity. Thanks for sharing!

CollapseExpand
 
abrahamn profile image
Abraham
Love things thought provoking
  • Location
    Johannesburg, South Africa
  • Work
    Software Engineer
  • Joined

This reminds me of back in the day when XML support was added to MSSQL, now we could store SOAP responses directly into our columns, it was magic. Looking back, it was bloated and needed wizardry to parse elements. In fact i see a circle, soap+xml, rest+json, grpc+protobuf and now json-rpc+json

CollapseExpand
 
pthreat profile image
pthreat
Pff
  • Joined
• Edited on• Edited

Click baity title with no real heavy loaded aggregations example, nosql DBS shine best with heavy and complex searches. Also as others have stated what you are proposing here it's a trap that leads to no data normalization. In short it might seem attractive at the start, a nightmare at the end

CollapseExpand
 
axixu_e9850e79c76a6f profile image
Axixur
  • Joined

Mongodb can handle 65536 concurrent connections by default..postgresql 100, if you increase it, it affect performance.

Sharding is hard to implement in postgresql but in mongodb its super easy.

CollapseExpand
 
ron_johnson_7d711e3294d7e profile image
Ron Johnson
  • Joined

“By default”??? Changing the default number of connections is pretty darned trivial.

CollapseExpand
 
greg_lin_55e8ee12ecd79cd6 profile image
Mike
  • Joined

Absolutely brilliant breakdown.
As a developer who's worked extensively with both MongoDB and Postgres, I couldn't agree more — jsonb is criminally underrated. The combination of SQL power, schema flexibility, and indexing makes it a true Swiss army knife, especially for event logs, dynamic metadata, and hybrid schemas.
Also admire how you structured the migration Strategy — I’ve done similar for moving audit logs and form builders into Postgres, and performance actually improved once we tuned GIN indexes correctly.

I think PostgreSQL isn't just relational anymore — it's relational with superpowers. ✨

CollapseExpand
 
xak2000 profile image
Ruslan Stelmachenko
  • Joined

"Maybe I should just use MongoDB," you whisper to yourself.

Then you slap yourself into the face and get back to use a proper schema and relations in your relational DB. :)

CollapseExpand
 
68wooley profile image
Graeme Robinson
Dad, tech geek, NoSQL proponent, car guy, and trail runner - in no particular order.
  • Location
    Denver, Colorado
  • Education
    University of Strathclyde, Glasgow, Scotland
  • Pronouns
    He / Him / His
  • Work
    Staff Developer Advocate @ MongoDB
  • Joined

If you think MongoDB means no schema then - yeah - you probably should stick with an RDBMS.

CollapseExpand
 
stevsharp profile image
Spyros Ponaris
Tech Lead | Senior Software Engineer | .NET Enthusiast (BSc, MSc, MCP, MCSD)Contact Information:https://www.linkedin.com/in/spyros-ponaris-913a6937/
  • Email
  • Location
    Athens
  • Education
    BSc University of Sunderland- MSc University of Greenwich
  • Pronouns
    Tech Lead and Senior Software Engineer with a strong focus on .NET technologies.
  • Work
    Tech Lead | Senior Software Engineer |
  • Joined

Great Post. Have you benchmarked query performance between your Postgres **JSONB **model and the previous Mongo setup? Would love to know if Postgres held up under load.

CollapseExpand
 
ilumin profile image
Lumin
  • Location
    Bangkok, Thailand
  • Joined

I think JSONB works well for simple use cases like user or tenant settings that are mostly static - data that rarely changes and doesn't need aggregation or complex queries.

CollapseExpand
 
nikhil_pampatwar profile image
Nikhil Pampatwar
Technology Mentor & Entrepreneur
  • Joined

Sounds great. SQL & NoSQL together. 👌🏼👌🏼

CollapseExpand
 
ariba932 profile image
Kolawole Mobolaji
Just a dude that love 0 and 1
  • Location
    Nigeria
  • Education
    Wales University and Quantic Business School
  • Work
    Managing Partner at Tevc Concepts
  • Joined

I have been seeing jsonb all times, took it for just a json collector type. But you gave me beautiful insight into it value.. Thank you very much

CollapseExpand
 
jaycverg profile image
Jayrome
Stucked in Java development :)
  • Joined

Been doing this for over 5 years. Really handy and gets the job done.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp