Hiprup

What is the difference between embedding and referencing in MongoDB?

MongoDB models relationships in two ways — the choice depends on access patterns.

Embedding — nest related data inside the parent document. One read fetches everything. Best when the child is always read with the parent and doesn't grow unbounded.

Referencing — store the related document's _id in the parent (or vice versa). Use $lookup or a second query to fetch. Best when the relationship is many-to-many, the child is large, or it's queried independently.

  • 16 MB document limit — embedding unbounded arrays is dangerous.

  • Hybrid is fine — embed a few recent items, reference the rest.

// EMBEDDING — user with addresses
{
  _id: ObjectId('...'),
  name: 'John',
  email: 'john@test.com',
  addresses: [  // Embedded — always fetched with user
    { type: 'home', city: 'NYC', zip: '10001' },
    { type: 'work', city: 'LA', zip: '90001' }
  ]
}
// One query gets everything:
db.users.findOne({ _id: userId });

// REFERENCING — orders reference users
// users collection
{ _id: ObjectId('user1'), name: 'John' }

// orders collection
{ _id: ObjectId('order1'), userId: ObjectId('user1'), total: 99.99 }

// Need $lookup or two queries:
db.orders.aggregate([
  { $lookup: { from: 'users', localField: 'userId', foreignField: '_id', as: 'user' } }
]);

// HYBRID — embed summary, reference details
{
  _id: ObjectId('...'),
  title: 'Blog Post',
  author: { _id: ObjectId('author1'), name: 'John' }, // Embedded summary
  commentCount: 42,  // Denormalized count
  // Full comments in separate collection (could be thousands)
}

Embedding: addresses live inside the user document — one read gets everything. Referencing: orders store userId and need $lookup to get user details — two collections.

Hybrid: embed the author summary (name for display) but reference the full author document. commentCount is denormalized for fast display.

Embed for: one-to-few, always accessed together, owned by parent. Reference for: one-to-many, shared data, independently accessed.

The hybrid pattern (embed summary, reference details) shows advanced thinking. Know the 16MB document limit as the embedding constraint.