Embedding vs Referencing

Embedding and referencing are two ways to model relationships in MongoDB.

Embedding

Embedding stores related data inside the same document.

Example:

{
  "name": "Asha",
  "address": {
    "city": "Pune",
    "country": "India"
  }
}

Use embedding when:

Referencing

Referencing stores related data in another collection and keeps an id reference.

Example:

{
  "title": "MongoDB Basics",
  "authorId": "user_123"
}

Use referencing when:

Common mistake

Do not embed unbounded arrays.

Example: putting all comments forever inside one post document can become a problem as comments grow.

Interview answer

Embedding stores related data inside the same document and is good when data is owned and read together. Referencing stores related data separately and links by id, which is better for independent, reused, or unbounded data. The choice depends on access patterns and growth.