惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

M
MIT News - Artificial intelligence
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
Last Week in AI
Last Week in AI
S
SegmentFault 最新的问题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
人人都是产品经理
人人都是产品经理
WordPress大学
WordPress大学
The Cloudflare Blog
IT之家
IT之家
雷峰网
雷峰网
小众软件
小众软件
博客园 - 叶小钗
博客园 - 聂微东
爱范儿
爱范儿
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
博客园 - 【当耐特】
V
V2EX
博客园_首页
T
Tailwind CSS Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
From React to MongoDB: How I Learned Backend Design (The ...
vaibhavi suradkar · 2026-06-24 · via DEV Community

Two weeks ago, I was a React developer who thought backend was just "API that returns data."

I started building a social media project and hit two problems that made me realize how little I actually knew.

This is me documenting that journey — what confused me, what I got wrong, and what finally clicked. If you're transitioning from frontend too, you'll probably face the same "wait, what?" moments.

The First Problem: Embedding vs Referencing

Even coming from a frontend background, I knew some backend basics. Enough to feel confident. Enough to think "I got this."

Then I decided to dig a rabbit hole.

I started learning about data modeling and schema design. Came across terms like embedding, referencing, schema design patterns. Read about them. Understood them individually. But something still felt off, like I was missing the actual instinct of when to use which. So I decided to put my hands on a project. Learning by doing.

So there I was, building a social media app. I had three things: Users, Posts, Comments. Simple, right?

Nope.

First real decision: should I embed posts inside the user document or create separate collections and reference them?

I had genuinely no idea. Both sounded fine to me. I almost embedded everything. posts inside users, comments inside posts because that felt "organized." Like keeping things together in one place.

A senior dev would have facepalmed so hard.

Here's what was actually confusing me: I was thinking about how data looks, not how the app uses it. That's where access patterns come into play.

My frontend brain thinks in components. Keep related things together, it makes UI easier. That instinct betrayed me in MongoDB.

The real question isn't "are posts related to users?" , of course they are! The real question is:

  • How often do I fetch posts separately?
  • Will this array grow unboundedly?
  • Is this data shared across multiple documents?

Posts grow over time, a user could have 5 posts or 500. Embedding them inside the user document means that document gets bigger every time someone posts. As the document grows, you risk hitting MongoDB's 16MB document limit and creating performance problems long before you get there.

So I referenced them instead:

// User Schema — stores only reference IDs
const userSchema = new mongoose.Schema({
  username: String,
  email:    String,
  posts:    [{ type: mongoose.Schema.Types.ObjectId, ref: "Post" }]
})

// Post Schema — stores who created it
const postSchema = new mongoose.Schema({
  user:    { type: mongoose.Schema.Types.ObjectId, ref: "User" },
  content: String,
  likes:   [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }]
})

Then used populate() to fetch actual post data when needed.

The lesson:

Access patterns matter more than how data "naturally belongs together." Don't think like a UI developer. Think about how the data grows and how the app reads it with scalability in mind from the start.

The Second Problem: Pagination

Days later, I hit pagination.

I thought skip/limit was just "skip some, take some. Easy."

Nope again.

I built it and it worked. Page 1? perfect. Page 2? fine. Page 50? Suddenly the server was sweating.

Here's what I didn't understand: skip() doesn't actually skip documents. It reads them and throws them away.

So on page 50 with 10 posts per page: MongoDB reads 490 documents → throws them away → returns next 10

Every page deeper you go, more wasted reads. Performance gets worse linearly. On small datasets you never notice. On real data with thousands of posts, it's a problem!

I also got the formula wrong the first time:

const page = req.query.page || 0
.skip(page * postsPerPage)  // starts from page 0 — weird

// What actually makes sense
const page = parseInt(req.query.page) || 1
const skip = (page - 1) * postsPerPage
// page 1 → skip 0  → posts 1-10
// page 2 → skip 10 → posts 11-20
// page 3 → skip 20 → posts 21-30

Page starting at 0 works technically, but page 0 makes no sense to a user. Page 1 is natural. Small thing, real difference.

The correct implementation:

const page         = parseInt(req.query.page) || 1
const postsPerPage = 10
const skip         = (page - 1) * postsPerPage

const posts = await postModel
  .find({ user: user._id })
  .sort({ createdAt: -1 })  
  .skip(skip)
  .limit(postsPerPage)
  .lean()                   

const totalPosts = await postModel.countDocuments({ user: user._id })
const totalPages = Math.ceil(totalPosts / postsPerPage)

When does it matter? If your dataset is small, under a few thousand documents skip/limit is honestly fine. It becomes a real problem at scale, thousands of pages deep, millions of documents. That's also why many large applications eventually move to cursor-based pagination, which avoids the growing cost of deep skips entirely.

But understanding why it gets slow is what separates someone who just makes it work from someone who understands what they built.

The Bigger Lesson

These two problems taught me something I didn't expect.

Backend isn't just "make the code work." It's "make it work fast, make it secure, make it scalable."

Frontend taught me to think about UI, about the user's perspective, that 30-second first impression rule, what they see before they even read a word.

Backend is teaching me to think about data, not how it looks on screen, but how it lives in a database, how it grows over time, how the application actually uses it at scale.

Different brains. Same craft.

If You're Making This Transition Too

The confusion is normal.
The problems are common.
And the learning is real.

I'm documenting this journey as I go, not because I'm an expert, but because these are the things that genuinely tripped me up while building projects.
If sharing this saves someone a few hours of debugging, that's a win.

If you're making the jump from frontend to backend too, I'd love to hear what concepts challenged you the most. What was your first "wait, what?" moment?