Include Order Items
In this step, we'll enhance our order schema by adding a comprehensive list of order items. We'll demonstrate how to create an array of items within our order document, capturing detailed product information.
Ensure you're still in the MongoDB shell:
mongosh ecommerce_orders
Now, let's update our order document to include a detailed items array:
db.orders.updateOne(
{ order_id: "ORD-2024-001" },
{
$set: {
items: [
{
product_id: "PROD-001",
name: "Wireless Headphones",
category: "Electronics",
price: 129.99,
quantity: 1,
subtotal: 129.99
},
{
product_id: "PROD-002",
name: "Laptop Sleeve",
category: "Accessories",
price: 24.5,
quantity: 2,
subtotal: 49.0
}
],
total_items: 2,
subtotal: 178.99,
tax: 14.32,
total: 193.31
}
}
);
Let's verify the updated document:
db.orders.findOne({ order_id: "ORD-2024-001" });
Key features of this items schema:
- Array of items with detailed product information
- Each item includes product ID, name, category, price, and quantity
- Calculated subtotals for individual items
- Overall order totals (items count, subtotal, tax, total)
The array structure allows for multiple items in a single order and provides flexibility for different product types.