0% found this document useful (0 votes)
13 views3 pages

Embedded Document Example in Mongodb

The document explains how to create embedded documents in MongoDB, particularly for an e-commerce application involving Users, Orders, and Products. It highlights the benefits of data locality and simplicity, provides examples of MongoDB commands for inserting and retrieving embedded documents, and discusses considerations such as document size and update complexity. Overall, using embedded documents can enhance performance for closely related and frequently accessed data.

Uploaded by

suman_nayak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Embedded Document Example in Mongodb

The document explains how to create embedded documents in MongoDB, particularly for an e-commerce application involving Users, Orders, and Products. It highlights the benefits of data locality and simplicity, provides examples of MongoDB commands for inserting and retrieving embedded documents, and discusses considerations such as document size and update complexity. Overall, using embedded documents can enhance performance for closely related and frequently accessed data.

Uploaded by

suman_nayak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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": "alice@example.com",
"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": "alice@example.com",
"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": "alice@example.com",
"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"
}
}
});

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy