Lesson 9 of 14
One-to-Many Patterns: Bucket, Array, Sub-collection - 008
Okka 'One-to-Many' problem ki 4 solutions unnay. Meeru eppudu 'Array of ObjectIds' eh vaduthunnara? Aithe mee app slow avvadam khayam.
Core Explanation:
Embedding: Few items (Addresses). Fast reads.
Referencing (Parent -> Child): Many items, but unbounded (Comments). Child holds Parent ID.
Array of References (Child -> Parent): Limited cardinality (Tags).
Bucket Pattern: Time-series or grouped data. Group many small docs into one medium doc.
Wrong Practice: Predicting array size wrong.
// Product
{
"_id": "p1",
"reviews": [oid1, oid2, ... oid10000?] // Dangerous
}
Best Practice: Child holds parent reference for unlimited growth.
// Review Document
{
"_id": "r1",
"product_id": "p1", // Reference to parent
"text": "Good product"
}
db.reviews.createIndex({ product_id: 1 }) // Fast lookup
Closing Insight: "Few? Embed. Many? Child Reference. Huge? Bucket Pattern."