Open In App

Embedded vs. Referenced Documents in MongoDB

Last Updated : 30 Sep, 2025
Comments
Improve
Suggest changes
2 Likes
Like
Report

Both embedded documents and referenced documents are ways to model relationships in MongoDB, but they serve different purposes and have distinct advantages.

Embedded documents store related data within a single document, providing faster reads and simpler queries, while referenced documents link data across multiple collections, making it easier to manage large or complex datasets.

Here is a detailed comparison of MongoDB and MySQL based on various features:

Embedded DocumentsReferenced Documents
Documents within documents, creating a hierarchical structureDocuments refer to other documents stored in different collections
Supports atomic operations on the entire documentAtomic operations are limited to individual documents
Faster, as all related data is fetched in a single querySlower, requires multiple queries to fetch related data
Potentially slower for large documents due to size limitationsPotentially faster for large datasets due to smaller document sizes
High, as related data is stored togetherLow, related data is distributed across collections
Limited by MongoDB's 16 MB document size limitNot constrained by individual document size, better for large datasets
Can lead to data duplication and increased storage requirementsMinimizes data duplication, reducing storage requirements
Simple for atomic updates, but complex for deeply nested updatesComplex, especially when maintaining consistency across documents
Less flexible, better for fixed or simple hierarchical structuresMore flexible, suitable for complex and evolving schemas
- One-to-One Relationships<br>- One-to-Many Relationships (small datasets)- Many-to-Many Relationships<br>- Large Subdocuments<br>- Independent updates
json { "_id": 1, "name": "John Doe", "address": { "street": "123 Main St", "city": "Anytown", "state": "CA" } }Users Collection: json { "_id": 1, "name": "John Doe", "address_id": 1001 } Addresses Collection: json { "_id": 1001, "street": "123 Main St", "city": "Anytown", "state": "CA" }

Embedded Documents

Embedded documents are documents stored within other documents, forming a nested, hierarchical structure. This approach allows MongoDB to store related data together, making it easy to retrieve the entire set of information in a single query. This method uses MongoDB's support for complex document structures.

Use Cases

  • One-to-One Relationships: Where one document directly relates to another (e.g., user profile and user settings).
  • One-to-Many Relationships: For example, storing multiple comments under a single blog post where the comments are not too large.

Example of Embedded Document:

{
"_id": 1,
"name": "John Doe",
"address": {
"street": "123 Main St",
"city": "Anytown",
"state": "CA"
}
}
Screenshot-2024-06-01-161121
Post document

Query:

db.posts.findOne({"_id":ObjectId("665ef4b5b6034dde77877b86")})

Output

Screenshot-2024-06-04-163935
Embedded vs. Referenced Documents in MongoDB

Referenced Documents

Referenced documents store relationships by including a reference (usually an ObjectId) to another document stored in a different collection. This approach separates related data into distinct documents and collections.

Use Cases

  • Many-to-Many Relationships: For example, a student can enroll in multiple courses, and each course can have many students. References are ideal for these relationships.
  • Large Subdocuments: If the data is large or accessed independently, referencing is a better option. For example, storing product reviews in a separate collection.

Users Collection:

{
"_id": 1,
"name": "John Doe",
"address_id": 1001
}

Addresses Collection:

{
"_id": 1001,
"street": "123 Main St",
"city": "Anytown",
"state": "CA"
}
Screenshot-2024-06-01-161823
Post document

Query:

db.posts.find({"_id":ObjectId("665ef4b5b6034dde77877b86")}) 

Output

Screenshot-2024-06-04-164822
Embedded vs. Referenced Documents in MongoDB
db.comments.find({})
Screenshot-2024-06-04-165140
Embedded vs. Referenced Documents in MongoDB

Choosing the Right Approach for Relationships in MongoDB

When deciding between embedded and referenced documents, consider the following factors:

1. Access Patterns:

  • If related data is frequently accessed together, embedding is more efficient.
  • If the data is accessed independently or only occasionally, referencing is better.

2. Data Size and Growth:

  • For smaller or growing datasets, embedding may be sufficient.
  • For large or expanding datasets, referencing helps manage document size and complexity.

3. Update Frequency:

  • Embedding is ideal when you need atomic updates to related data.
  • Referencing is better when different parts of the data are updated frequently and independently.

4. Schema Complexity:

  • Simple, hierarchical data models can benefit from embedding.
  • Complex, evolving relationships are better managed using referencing.



Article Tags :

Explore