Embedded vs. Referenced Documents in MongoDB
Last Updated :
30 Sep, 2025
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 Documents | Referenced Documents |
---|
Documents within documents, creating a hierarchical structure | Documents refer to other documents stored in different collections |
Supports atomic operations on the entire document | Atomic operations are limited to individual documents |
Faster, as all related data is fetched in a single query | Slower, requires multiple queries to fetch related data |
Potentially slower for large documents due to size limitations | Potentially faster for large datasets due to smaller document sizes |
High, as related data is stored together | Low, related data is distributed across collections |
Limited by MongoDB's 16 MB document size limit | Not constrained by individual document size, better for large datasets |
Can lead to data duplication and increased storage requirements | Minimizes data duplication, reducing storage requirements |
Simple for atomic updates, but complex for deeply nested updates | Complex, especially when maintaining consistency across documents |
Less flexible, better for fixed or simple hierarchical structures | More 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"
}
}
Post documentQuery:
db.posts.findOne({"_id":ObjectId("665ef4b5b6034dde77877b86")})
Output
Embedded vs. Referenced Documents in MongoDBReferenced 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"
}
Post documentQuery:
db.posts.find({"_id":ObjectId("665ef4b5b6034dde77877b86")})
Output
Embedded vs. Referenced Documents in MongoDBdb.comments.find({})
Embedded vs. Referenced Documents in MongoDBChoosing 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.
Explore
MongoDB Tutorial
7 min read
Introduction
Installation
Basics of MongoDB
MongoDB Methods
Comparison Operators
Logical Operators
Arithmetic Operators
Field Update Operators
Array Expression Operators