Bird
Raised Fist0
MongoDBquery~20 mins

Embedding vs referencing decision in MongoDB - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
MongoDB Embedding vs Referencing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
When to use embedding in MongoDB?
Which scenario is best suited for embedding documents in MongoDB?
AWhen data needs to be shared across multiple collections
BWhen related data is very large and updated independently
CWhen related data is frequently accessed together and has a one-to-few relationship
DWhen data size exceeds MongoDB document size limit
Attempts:
2 left
💡 Hint
Think about data that is tightly connected and often retrieved in one go.
🧠 Conceptual
intermediate
2:00remaining
When to use referencing in MongoDB?
Which situation calls for referencing documents instead of embedding in MongoDB?
AWhen related data is large or shared among many documents
BWhen related data is small and rarely updated
CWhen data is always accessed together
DWhen data fits comfortably within a single document
Attempts:
2 left
💡 Hint
Consider data that is reused or updated separately.
❓ query_result
advanced
3:00remaining
Output of embedding vs referencing query
Given two collections, users with embedded addresses and orders referencing users, what is the output of this query?
MongoDB
db.orders.aggregate([
  { $lookup: {
      from: 'users',
      localField: 'user_id',
      foreignField: '_id',
      as: 'user_info'
  }},
  { $unwind: '$user_info' },
  { $project: {
      order_id: 1,
      'user_info.name': 1,
      'user_info.address.city': 1
  }}
])
A[]
B[{ order_id: 101, user_info: { name: 'Alice', address: { city: 'Seattle' } } }, { order_id: 102, user_info: { name: 'Bob', address: { city: 'Austin' } } }]
C[{ order_id: 101, user_info: { address: { city: 'Seattle' } } }, { order_id: 102, user_info: { address: { city: 'Austin' } } }]
D[{ order_id: 101, user_info: { name: 'Alice' } }, { order_id: 102, user_info: { name: 'Bob' } }]
Attempts:
2 left
💡 Hint
Look at the $project stage selecting name and address.city fields.
❓ schema
advanced
3:00remaining
Choose the best schema design for a blog with comments
You are designing a MongoDB schema for a blog where each post can have hundreds of comments. Which design is best?
AStore comments in a separate collection referencing the post
BEmbed only the first 10 comments inside the post, rest in a separate collection
CEmbed all comments inside the post document
DStore comments as an array of comment IDs inside the post document
Attempts:
2 left
💡 Hint
Consider document size limits and update frequency.
❓ optimization
expert
4:00remaining
Optimizing queries with embedding vs referencing
You have a MongoDB collection of products and a collection of reviews referencing products. You want to optimize read performance for showing product details with reviews. Which approach is best?
AStore review IDs inside product and fetch reviews separately in application code
BKeep reviews in a separate collection and use $lookup to join at query time
CEmbed all reviews inside each product document
DEmbed only the latest 3 reviews inside product, keep others separate
Attempts:
2 left
💡 Hint
Balance document size and query speed for common use cases.

Practice

(1/5)
1. Which scenario is best suited for embedding related data in MongoDB?
easy
A. Storing a user's profile and their list of addresses when addresses are few and rarely shared.
B. Storing product details and linking to multiple categories by ID.
C. Storing large documents that are frequently updated independently.
D. Storing shared tags used by many blog posts.

Solution

  1. Step 1: Understand embedding use case

    Embedding is best when related data is small and tightly coupled, like a user's addresses.
  2. Step 2: Analyze options for embedding

    Storing a user's profile and their list of addresses when addresses are few and rarely shared fits because addresses are few and not shared, so embedding improves read speed.
  3. Final Answer:

    Storing a user's profile and their list of addresses when addresses are few and rarely shared. -> Option A
  4. Quick Check:

    Embedding = small, tightly related data [OK]
Hint: Embed when data is small and tightly linked [OK]
Common Mistakes:
  • Embedding large or shared data causing duplication
  • Referencing small, simple data unnecessarily
2. Which of the following is the correct way to reference a document in MongoDB?
easy
A. { "author_id": ObjectId("507f1f77bcf86cd799439011") }
B. { "author": { "$ref": "users", "$id": ObjectId("507f1f77bcf86cd799439011") } }
C. { "author": ObjectId("507f1f77bcf86cd799439011") }
D. { "author": "507f1f77bcf86cd799439011" }

Solution

  1. Step 1: Identify referencing syntax

    Referencing stores the related document's ID as a field, usually named with _id suffix.
  2. Step 2: Check options for correct ID reference

    { "author_id": ObjectId("507f1f77bcf86cd799439011") } correctly stores the ObjectId with a clear field name author_id.
  3. Final Answer:

    { "author_id": ObjectId("507f1f77bcf86cd799439011") } -> Option A
  4. Quick Check:

    Reference = store related document's ID [OK]
Hint: Reference by storing ObjectId in a field ending with _id [OK]
Common Mistakes:
  • Using string instead of ObjectId for references
  • Using $ref and $id without DBRef context
  • Embedding full document instead of referencing
3. Given a MongoDB collection orders where each order embeds a small array of items, what is the expected result of querying an order by ID?
medium
A. Only the order document without any items data.
B. An error because embedded arrays cannot be queried.
C. The order document with all embedded items included in the result.
D. A list of item documents from a separate collection.

Solution

  1. Step 1: Understand embedding query behavior

    When data is embedded, querying the parent document returns all embedded data inside it.
  2. Step 2: Analyze query result for embedded items

    Querying by order ID returns the order document including the embedded items array.
  3. Final Answer:

    The order document with all embedded items included in the result. -> Option C
  4. Quick Check:

    Embedding returns full embedded data [OK]
Hint: Embedded data returns inside parent document query [OK]
Common Mistakes:
  • Expecting separate documents for embedded data
  • Thinking embedded arrays cause query errors
  • Confusing referencing with embedding results
4. You have a MongoDB schema where user documents embed an array of posts. You notice performance issues when updating posts frequently. What is the likely cause and fix?
medium
A. Indexing embedded arrays causes slow updates; remove indexes.
B. Referencing posts causes slow updates; embed posts instead.
C. Using ObjectId for posts causes slow updates; use strings instead.
D. Embedding large arrays causes slow updates; switch to referencing posts separately.

Solution

  1. Step 1: Identify embedding impact on updates

    Embedding large or frequently updated arrays causes the whole document to be rewritten, slowing updates.
  2. Step 2: Choose referencing to improve update performance

    Referencing posts separately allows updating posts without rewriting the entire user document.
  3. Final Answer:

    Embedding large arrays causes slow updates; switch to referencing posts separately. -> Option D
  4. Quick Check:

    Large/frequent updates = use referencing [OK]
Hint: Large, frequently updated data? Use referencing [OK]
Common Mistakes:
  • Embedding large arrays despite frequent updates
  • Ignoring update performance impact
  • Misunderstanding ObjectId role in updates
5. You are designing a MongoDB schema for a blogging platform. Each blog post has many comments, and comments can be edited or deleted independently. Also, comments can be very large in number. What is the best design choice?
hard
A. Embed all comments inside each blog post document.
B. Reference comments in a separate collection linked by post ID.
C. Embed only the first 10 comments and reference the rest.
D. Store comments as a JSON string inside the blog post.

Solution

  1. Step 1: Analyze comment characteristics

    Comments are numerous, large, and updated independently, so embedding would cause large documents and slow updates.
  2. Step 2: Choose referencing for scalability and independent updates

    Referencing comments in a separate collection allows efficient updates and avoids large blog post documents.
  3. Final Answer:

    Reference comments in a separate collection linked by post ID. -> Option B
  4. Quick Check:

    Large, independent data = referencing [OK]
Hint: Large, independent data? Reference separately [OK]
Common Mistakes:
  • Embedding large, frequently updated arrays
  • Mixing embedding and referencing without clear rules
  • Storing JSON strings instead of structured data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard