← Back to MongoDB Course | Chapter 9: Schema Design | Lesson 4 of 7

Data Modeling के Patterns

subset, bucket, computed, और extended reference जैसे named patterns common modelling problems solve करते हैं।

In this page:

  1. Data Modeling Patterns

Data Modeling Patterns

Subset pattern एक बड़े array के सिर्फ most-used हिस्से को embed करता है। Bucket pattern time-series items को प्रति period एक document में group करता है।

Computed pattern precomputed totals store करता है, और extended reference joins से बचने के लिए किसी दूसरे document से कुछ fields copy करता है।

Note: जो आप अक्सर पढ़ते हैं वह precompute करें और इसे write पर update करें।

उदाहरण: Data modeling patterns

bash
// Bucket pattern: one document per sensor per hour instead of one per reading
test> db.readings.insertOne({ _id: "s1-2024010510", sensor: "s1", count: 0, sum: 0, values: [] })
{ acknowledged: true, insertedId: 's1-2024010510' }
test> db.readings.updateOne({ _id: "s1-2024010510" }, { $push: { values: 21.5 }, $inc: { count: 1, sum: 21.5 } })
{ acknowledged: true, matchedCount: 1, modifiedCount: 1 }
test> db.readings.updateOne({ _id: "s1-2024010510" }, { $push: { values: 22.5 }, $inc: { count: 1, sum: 22.5 } })
{ acknowledged: true, matchedCount: 1, modifiedCount: 1 }
test> db.readings.aggregate([{ $project: { _id: 0, sensor: 1, count: 1, avg: { $divide: ["$sum", "$count"] } } }])
[ { sensor: 's1', count: 2, avg: 22 } ]

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. बहुत सारी fields copy करना
  2. computed values कभी refresh न करना
  3. ज़रूरत measure करने से पहले patterns इस्तेमाल करना
चैप्टर सारांश
  • Subset hot part embed करता है
  • Bucket time-series data group करता है
  • Computed totals store करता है
  • Extended reference key fields copy करता है

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.