Embedding vs. Referencing Basics
In MongoDB, which use case is generally best for embedding documents?
- Storing an address within a user document
- Linking a user to multiple orders across different collections
- Creating separate collections for user preferences
- Storing products and their reviews in different collections
- Using references for small, unchanging child objects
When to Use Referencing
Which scenario is a good case for referencing instead of embedding documents in MongoDB?
- When child documents are accessed independently from the parent
- When the child object is always loaded with the parent
- For small, rarely changing data tightly related to the parent
- When you want to avoid using ObjectId fields
- If parent and child data must always be updated together
Choosing Collections
What is a recommended practice when creating collections in MongoDB for a blog with posts and comments?
- Embed comments within each post document
- Store each comment in a separate collection
- Put both posts and comments in a single 'postscomments' colllection
- Store comments as separate databases
- Avoid collections and use JSON files instead
Dealing With Document Growth
If a MongoDB document could grow unbounded over time (such as a user with messages), what is the best modeling choice?
- Reference messages from the user document
- Embed all messages inside the user document
- Create a capped collection for users
- Store user messages in memory only
- Keep messages as a single field in the user record
Modeling Many-to-Many Relationships
Suppose you are modeling a 'students' and 'courses' relationship, where each student can enroll in many courses and each course can have many students. What is a typical approach?
- Reference course Ids in the student document and student Ids in the course document
- Embed all students inside each course document
- Create one collection for both students and courses mixed together
- Only store course Ids within students and not vice versa
- Create a flat collection without nesting or references
Minimizing Write Operations
Which modeling choice can help minimize write operations in a high-traffic e-commerce application?
- Store product reviews in a separate 'reviews' collection and reference them from products
- Embed all reviews directly inside each product document, regardless of review count
- Delete and rewrite entire product documents on each review
- Avoid referencing and use just arrays of review IDs
- Store reviews in the same collection as products
Denormalization in MongoDB
Why might you choose to denormalize data by embedding documents in MongoDB?
- To improve read performance by reducing the number of queries
- To enforce strict relational constraints
- To avoid using ObjectId fields
- To decrease storage usage at any scale
- Because normalization is always slower in MongoDB
Implications of Large Document Size
What problem can arise if you embed too much data within a single MongoDB document?
- You might exceed the 16MB document size limit
- Queries will always run faster
- Index sizes will shrink
- You can store unlimited child documents per parent
- Referential integrity is automatically enforced
Code: Referencing Example
Given this scenario:nnusers: { _id: 1, name: 'Eva', favoriteBook: ObjectId('abcd1234') }nbooks: { _id: ObjectId('abcd1234'), title: 'MongoDB Guide' }nnWhat modeling approach is being used for favorite books?
- Referencing
- Emedding
- Normalizzation
- Flat data
- Mirroring
Choosing Between Embedding and Referencing
If a web application needs fast access to a user's profile along with their social media links, which data model is generally suitable?
- Embed social media links inside the user document
- Reference each link in a separate collection
- Store all users and all links in the same collection
- Use one huge document for all user profiles and links
- Save links as a comma-separated string