Skip to content
HomeAboutSkillsExperienceProjectsBlogContact

2026-01-15

Migrating 30+ Models from MongoDB to PostgreSQL Without Losing Data

This is an example post to show how the blog works. Write in plain markdown here, mdsvex compiles this file straight into a Svelte component at build time, no CMS, no database, no runtime cost.

Why migrate at all

MongoDB’s schemaless documents are great for moving fast early on. Once a product’s data has real relationships and needs transactional guarantees, those benefits start working against you.

The approach

  1. Model the target schema in Prisma first, as the source of truth.
  2. Write a one-off migration script that reads each Mongo collection and inserts into the matching Postgres table.
  3. Run it against a copy of production data, diff row counts, then run it for real.
// a small taste of the migration script shape
const users = await mongo.collection('users').find().toArray();
for (const user of users) {
  await prisma.user.create({ data: mapUserToPostgres(user) });
}

Delete this file (or replace its contents) and drop new .md files into src/lib/content/blog/ for future posts, each one automatically gets its own page and shows up in the blog index and the sitemap.

← Back to all posts