Lesson 22 of 22 · NoSQL

Embedding vs. References

Embedding vs. references: learn how to model data in a document database.

Embedding (Denormalization)

Store related data in one document:

{ name: "Alice", orders: [{ product: "Widget" }] }

References (Normalization)

Store IDs pointing to other collections:

{ _id: "u1", name: "Alice" }
{ user_id: "u1", product: "Widget" }

When to Embed

  • Data accessed together
  • One-to-few relationships

When to Reference

  • Large sub-documents
  • Many-to-many relationships

Your challenge

Insert a customer named Sam Lee whose address is an embedded document with city "Springfield".

Solve it in your browser

Free, no sign-up. The NoSQL database runs in your browser; your answer is checked instantly, with hints if you need them.