Creating an embedded document in MongoDB involves nesting documents within
other documents. This approach is useful for data that is frequently accessed
together and has a clear hierarchical relationship. Here’s an example of an
embedded document model for an e-commerce application involving `Users`,
`Orders`, and `Products`.
### Users Collection with Embedded Orders
```json
{
"_id": ObjectId("60d5ecf7b2e24b3dc8c6123b"),
"name": "Alice Smith",
"email": "
[email protected]",
"address": "123 Maple Street",
"orders": [
{
"order_id": ObjectId("60d5ecf7b2e24b3dc8c6123e"),
"order_date": ISODate("2023-07-20T10:30:00Z"),
"status": "Shipped",
"products": [
{
"product_id": ObjectId("60d5ecf7b2e24b3dc8c6123c"),
"name": "Laptop",
"quantity": 1,
"price": 1200.00
},
{
"product_id": ObjectId("60d5ecf7b2e24b3dc8c6123d"),
"name": "Smartphone",
"quantity": 2,
"price": 800.00
}
],
"total": 2800.00
}
]
}
```
### Explanation
1. **Users Collection**: Contains user details, including an array of embedded
`orders`.
2. **Orders**: Each order is embedded directly within the user document. It includes
order details such as `order_id`, `order_date`, `status`, `products`, and `total`.
3. **Products**: Each product within an order is also embedded. It includes details
like `product_id`, `name`, `quantity`, and `price`.
### Benefits of Embedded Documents
- **Data Locality**: Since related data is stored together, read operations are faster
because all necessary data is in a single document.
- **Simplicity**: The data model is simpler and easier to understand as related data
is nested directly.
### Example MongoDB Commands to Insert an Embedded Document
Here's how you might insert a user document with an embedded order using
MongoDB shell commands:
```javascript
db.users.insertOne({
"_id": ObjectId("60d5ecf7b2e24b3dc8c6123b"),
"name": "Alice Smith",
"email": "[email protected]",
"address": "123 Maple Street",
"orders": [
{
"order_id": ObjectId("60d5ecf7b2e24b3dc8c6123e"),
"order_date": ISODate("2023-07-20T10:30:00Z"),
"status": "Shipped",
"products": [
{
"product_id": ObjectId("60d5ecf7b2e24b3dc8c6123c"),
"name": "Laptop",
"quantity": 1,
"price": 1200.00
},
{
"product_id": ObjectId("60d5ecf7b2e24b3dc8c6123d"),
"name": "Smartphone",
"quantity": 2,
"price": 800.00
}
],
"total": 2800.00
}
]
});
```
### Example Query to Retrieve User with Embedded Orders
To retrieve a user with their embedded orders, you can use a simple `find` query:
```javascript
db.users.findOne({ _id: ObjectId("60d5ecf7b2e24b3dc8c6123b") });
```
This query will return the user document along with all embedded orders and
products.
### Considerations
- **Document Size**: MongoDB documents have a size limit of 16MB. Ensure that
the embedded documents do not cause the total document size to exceed this limit.
- **Data Duplication**: If the same embedded documents are used in multiple
places, consider the trade-offs between embedding and referencing to avoid
excessive data duplication.
- **Update Complexity**: Updating nested documents can be more complex and
may require using positional `$` operators or array filters.
Using embedded documents can greatly enhance performance and simplify your
data model for scenarios where data is closely related and frequently accessed
together.
db.users.insertOne({
"_id": 10010,
"name": "Alice Smith",
"email": "[email protected]",
"addresses": {
"permanent": {
"street": "123 Maple Street",
"city": "Springfield",
"state": "IL",
"zip": "62701",
"country": "USA"
},
"contractual": {
"street": "456 Elm Street",
"city": "Shelbyville",
"state": "IL",
"zip": "62702",
"country": "USA"
}
}
});