Embedding बनाम Referencing
साथ पढ़ा जाने वाला data embed करें और shared, बड़ा, या independently बदलने वाला data reference करें।
In this page:
Embedding बनाम Referencing
Embedding fast single-read access के लिए related data को एक document के अंदर store करता है। Referencing ids store करता है और इन्हें एक दूसरी query या $lookup से lookup करता है।
One-to-few, owned, read-together data के लिए embed करें; बड़े, shared, या अक्सर बदलने वाले data के लिए reference करें।
Note:
अपनी application data कैसे पढ़ती है इसके आसपास design करें, table diagrams के आसपास नहीं।
उदाहरण: Embedding vs referencing
test> db.users.insertOne({ _id: 1, name: "Ada", address: { city: "London", zip: "N1" } })
{ acknowledged: true, insertedId: 1 }
test> db.authors.insertOne({ _id: 7, name: "Ada" })
{ acknowledged: true, insertedId: 7 }
test> db.books.insertOne({ _id: 100, title: "Notes", authorId: 7 })
{ acknowledged: true, insertedId: 100 }
test> db.books.aggregate([{ $lookup: { from: "authors", localField: "authorId", foreignField: "_id", as: "author" } }, { $project: { _id: 0, title: 1, author: { $first: "$author.name" } } }])
[ { title: 'Notes', author: 'Ada' } ]
⚠️ 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. #}
आम गलतियां
- Unbounded lists embed करना
- एक relational database की तरह सब कुछ reference करना
- अक्सर बदलने वाला data duplicate करना
चैप्टर सारांश
- साथ पढ़ा data embed करें
- Shared या बड़ा data reference करें
- 16 MB document limit पर ध्यान दें
- Query patterns के लिए design करें
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: