MongoDB Schema Design for MERN Apps: Embed or Reference?

By Muhammad Osama Khan, Software Engineer. Database, Oct 13, 2026, 3 min read.

Embed what you always read together and what stays small; reference what grows or is shared. Worked examples with Mongoose models.

MongoDB has no joins you are forced to use, so the schema decision that matters most is simple: does this data live inside the parent document, or in its own collection with a reference? Get it right and most queries become one read. Get it wrong and you either fetch too much or fight document growth.

A rule of thumb

  • Embed when the child is read together with the parent, belongs to only one parent, and stays small and bounded (an address, a handful of tags, order line items).
  • Reference when the child grows without limit (comments, activity logs), is shared by many parents (a user referenced by many posts), or is read and updated on its own.
  • Ask "how many?" first. One to a few: embed. Thousands: reference. Unbounded: always reference.

Embedding: an order and its items

An order is displayed with its items and never edited line by line afterwards, so the items belong inside it. Copy the price at purchase time: if the product price changes later, old orders must not.

server/models/Order.js

import mongoose from 'mongoose';

const itemSchema = new mongoose.Schema(
  {
    product: { type: mongoose.Schema.Types.ObjectId, ref: 'Product', required: true },
    name: { type: String, required: true },       // snapshot of the name at purchase time
    unitPrice: { type: Number, required: true },  // snapshot of the price (in the smallest unit)
    quantity: { type: Number, required: true, min: 1 }
  },
  { _id: false }
);

const orderSchema = new mongoose.Schema(
  {
    customer: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true, index: true },
    items: { type: [itemSchema], validate: (v) => v.length > 0 },
    status: { type: String, enum: ['pending', 'paid', 'shipped'], default: 'pending' }
  },
  { timestamps: true }
);

export default mongoose.model('Order', orderSchema);

Referencing: posts and comments

A popular post can collect thousands of comments. Embedding them would push the document towards MongoDB's 16 MB limit and force you to load all of them just to show the title. Give comments their own collection and point back at the post.

server/models/Comment.js

const commentSchema = new mongoose.Schema(
  {
    post: { type: mongoose.Schema.Types.ObjectId, ref: 'Post', required: true },
    author: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
    body: { type: String, required: true, maxlength: 2000 }
  },
  { timestamps: true }
);

// the query you will run all the time: comments of a post, newest first
commentSchema.index({ post: 1, createdAt: -1 });

server/routes/comments.js

const comments = await Comment.find({ post: postId })
  .sort({ createdAt: -1 })
  .limit(20)
  .populate('author', 'name avatarUrl') // only the fields the UI shows
  .lean();

The snapshot pattern

Whenever a document must stay true to a moment in time (an invoice, an order, a booking), copy the values it needs instead of only referencing them. Keep the reference as well, so you can still link back to the live product or user.

Mistakes worth avoiding

  • Embedding an array that only ever grows. Check the worst case, not the average one.
  • Normalising everything like a SQL database and then needing five populate calls per request.
  • Storing money as floating point numbers. Use integers in the smallest unit (cents, paisa) or Decimal128.
  • Forgetting indexes on reference fields you filter by. A reference without an index is a collection scan waiting to happen (the next post covers this).

Frequently asked questions

Should I embed or reference documents in MongoDB?

Embed data that is read together with its parent, belongs to one parent and stays small (an address, order line items). Reference data that grows without limit (comments, logs), is shared by many parents, or is read and updated on its own.

What is the maximum size of a MongoDB document?

A single document can be at most 16 MB (the BSON document limit). An array that only ever grows can reach it, which is one reason unbounded lists belong in their own collection.

Is Mongoose populate the same as a SQL join?

No. populate runs a second query and combines the results in Node. It is fine for a page of results. For joins across many documents, use an aggregation with $lookup so the work happens inside the database.

How should I store prices in MongoDB?

Not as floating point numbers. Store integers in the smallest unit (cents or paisa), or use Decimal128. For orders and invoices, also copy the price into the order so later price changes do not alter old records.

Keep reading

All articles · Work with me

MO
Muhammad Osama Khan