Articles → MONGODB → One-To-Many Relationships In Mongodb
One-To-Many Relationships In Mongodb
As Embedded Document
{
"_id": 1,
"orderDate": ISODate("2023-04-13T00:00:00Z"),
"total": 100.0,
"lineItems": [
{
"productId": 1001,
"productName": "Product 1",
"quantity": 2,
"price": 25.0
},
{
"productId": 1002,
"productName": "Product 2",
"quantity": 1,
"price": 50.0
}
]
}
As Referenced Document
// posts collection
{
"_id": 1,
"title": "Post 1",
"body": "This is the body of post 1"
}
// comments collection
{
"_id": 1001,
"postId": 1,
"author": "John Doe",
"comment": "Great post!"
},
{
"_id": 1002,
"postId": 1,
"author": "Jane Smith",
"comment": "Thanks for sharing"
}