Document Structure
// Embedded Documents
db.orders.insertOne({
orderId: "12345",
customer: {
name: "John Doe",
email: "john@example.com",
address: {
street: "123 Main St",
city: "Boston",
state: "MA"
}
},
items: [
{ product: "Laptop", price: 999.99, quantity: 1 },
{ product: "Mouse", price: 24.99, quantity: 2 }
]
});
References
// Referenced Documents
db.products.insertOne({
_id: ObjectId("..."),
name: "Laptop",
price: 999.99
});
db.orders.insertOne({
customer_id: ObjectId("..."),
product_ids: [ObjectId("...")]
});
// Lookup referenced documents
db.orders.aggregate([
{
$lookup: {
from: "products",
localField: "product_ids",
foreignField: "_id",
as: "products"
}
}
]);