Embedded vs. Referenced Documents in MongoDB
Last Updated :
11 Feb, 2025
When designing a schema in MongoDB, understanding how to model relationships between data is critical for optimizing performance, scalability, and maintainability. MongoDB offers two primary ways to represent relationships: embedded documents and referenced documents. Both have their advantages and trade-offs and selecting the right model depends on the specific use case and requirements of your application.
In this article, we’ll explore the differences between embedded and referenced documents in MongoDB, when to use each approach, and how they impact data management and query performance.
Key Differences Between Embedded and Referenced Documents
Given below are the key differences between embedded and referenced documents in MongoDB, which help determine the best approach for our application based on performance, data size, and schema complexity. These differences highlight how data is stored and related in each model, along with their advantages and limitations.
Feature | Embedded Documents | Referenced Documents |
---|
Definition | Documents within documents, creating a hierarchical structure | Documents refer to other documents stored in different collections |
Atomic Operations | Supports atomic operations on the entire document | Atomic operations are limited to individual documents |
Read Performance | Faster, as all related data is fetched in a single query | Slower, requires multiple queries to fetch related data |
Write Performance | Potentially slower for large documents due to size limitations | Potentially faster for large datasets due to smaller document sizes |
Data Locality | High, as related data is stored together | Low, related data is distributed across collections |
Document Size | Limited by MongoDB's 16 MB document size limit | Not constrained by individual document size, better for large datasets |
Redundancy | Can lead to data duplication and increased storage requirements | Minimizes data duplication, reducing storage requirements |
Update Complexity | Simple for atomic updates, but complex for deeply nested updates | Complex, especially when maintaining consistency across documents |
Schema Flexibility | Less flexible, better for fixed or simple hierarchical structures | More flexible, suitable for complex and evolving schemas |
Use Cases | - One-to-One Relationships<br>- One-to-Many Relationships (small datasets) | - Many-to-Many Relationships<br>- Large Subdocuments<br>- Independent updates |
Example | 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" } |
Introduction to MongoDB Relationships
In MongoDB, relationships between documents can be managed either by embedding related data within a single document or by using references that point to other documents in separate collections. Understanding the pros and cons of these methods will help us design a database schema that is both efficient and scalable.
1. 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.
Advantages
- Atomic Operations: Since all related data is in one document, updates to this document are atomic, ensuring data consistency.
- Read Performance: Fetching all related data in a single query is faster and more efficient, as it avoids the need for multiple queries and joins.
- Data Locality: Keeping related data together can improve performance, particularly when the data is frequently accessed together.
Disadvantages
- Document Size: MongoDB has a document size limit of 16 MB. Embedding too much data can lead to large documents that may exceed this limit.
- Redundancy: Duplicating data across multiple documents can lead to redundancy and increased storage requirements.
- Complex Updates: Updating deeply nested structures can become complex and may require extensive manipulation of the document.
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 MongoDB2. 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.
Advantages
- Flexibility: Allows for more flexible and normalized data models, making it easier to manage complex relationships.
- Document Size Management: Keeps documents smaller, avoiding the 16 MB document size limit.
- Reduced Redundancy: Reduces data duplication by storing related data in separate documents.
Disadvantages
- Join Operations: Requires additional queries to fetch related data, which can impact read performance.
- Consistency: Maintaining consistency across referenced documents can be challenging, especially without transactions.
- Complex Queries: Queries involving multiple collections can become complex and may require careful indexing.
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.
Conclusion
Choosing between embedded and referenced documents in MongoDB involves evaluating the specific needs of our application, including performance, data size, and complexity. Embedded documents offer simplicity and performance benefits for related data accessed together, while referenced documents provide flexibility and scalability for more complex relationships. By understanding the trade-offs and use cases for each approach, we can design efficient and scalable MongoDB schemas tailored to your application's requirements.
Similar Reads
MongoDB - Embedded Documents
MongoDB provides you a cool feature which is known as Embedded or Nested Document. Embedded document or nested documents are those types of documents which contain a document inside another document. Or in other words, when a collection has a document, this document contains another document, anothe
2 min read
MongoDB - Query Embedded Documents Using Mongo Shell
MongoDB is a powerful NoSQL database that stores data in a flexible, document-based structure. One of its most useful features is embedded documents, which allow us to store related data inside a single document instead of using multiple collections and performing complex joins. In this article, we
5 min read
Sorting Documents in MongoDB
Sorting documents in MongoDB is the process of arranging data in a specific order based on field values, enhancing the efficiency and readability of query results. In this article, We will learn about Sorting Documents in MongoDB by understanding various examples in detail. Sorting Documents in Mong
4 min read
Mongoose Documents vs Models
Mongoose, a popular Node library, is widely used for interacting with MongoDB, a NoSQL database. Mongoose simplifies the process of working with MongoDB by providing an object data modeling (ODM) framework. In the Mongoose ecosystem, two key concepts are Documents and Models. In this article, we'll
4 min read
MongoDB Queries Document
MongoDB is NoSQL(Not only SQL) database because the storage and retrieval of data in MongoDB are not in the form of tables like in SQL. It stores data in a BSON structure. It uses BSON or Binary JSON data format to organize and store data. This data format includes all JSON data types and adds types
15+ min read
MongoDB - Query Documents using Mongo Shell
MongoDB provides you read operations to retrieve documents from the collection or query a collection for a document. You can perform read operations using the db.collection.find() method. This method selects or views the documents from the collection and returns the cursor to the selected document.
4 min read
How to replace one document in MongoDB using Node.js ?
MongoDB, the most popular NoSQL database, we can count the number of documents in MongoDB Collection using the MongoDB collection.countDocuments() function. The mongodb module is used for connecting the MongoDB database as well as used for manipulating the collections and databases in MongoDB. Insta
1 min read
MongoDB Relationships Embed or Reference
MongoDB is a popular NoSQL database that offers flexibility in managing data relationships. Understanding the different methods of creating relationships in MongoDB is essential for designing efficient and effective database schemas. In this article, We will explore two primary methods which are Emb
5 min read
What is the Format of Document in MongoDB?
MongoDB, which is one of the most prominent NoSQL databases, uses a document-oriented data model. A document in MongoDB is a collection of key-value pairs, where keys are strings (field names) and values can be of various data types such as strings, numbers, arrays, boolean values, dates, or even ne
4 min read
How to Find Documents by ID in MongoDB?
In MongoDB, finding documents by their unique identifier (ID) is a fundamental operation that allows us to retrieve specific records efficiently. Each document in a MongoDB collection has a unique _id field, which serves as the primary key. In this article, we will explore how to find documents by I
3 min read