MongoDB & MongooseInterview preparation
Foundation·Concept·2 min read

When should I embed data or reference it?

Practice this question

Definition:

Embedding stores related data inside one document, while referencing stores a link to data in another document. Embedding makes related reads and single-document atomic updates convenient, but can duplicate data or create oversized documents. Referencing supports independently managed data at the cost of additional reads or joins.

Key points:

  • Embed small related data that is normally read together, such as an order address.

  • Reference large, shared or independently changing data, such as products.

  • Avoid arrays that grow forever; one document has a 16 MiB size limit.

  • Ask whether related records grow without a bound, change independently or must be updated together.

Example:

JavaScript
const order = {
  customerId: 'customer-42',      // Reference to an independent record.
  shippingAddress: { city: 'Surat', postalCode: '395001' } // Snapshot.
};
  • The order keeps its delivery address even if the customer later edits their profile.

Interview point:

  • Base the choice on read/write patterns and lifecycle, not a rule that MongoDB always prefers embedding.
Adapted from 04_MONGODB_MONGOOSE.md · “When should I embed data or reference it?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.