0
0
MongoDBquery~20 mins

Joins vs embedding decision in MongoDB - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB Joins vs Embedding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
When to prefer embedding over referencing in MongoDB?

Which scenario best suggests using embedding documents instead of referencing (joins) in MongoDB?

AWhen data needs to be normalized to avoid duplication.
BWhen related data is very large and updated independently.
CWhen related data is frequently accessed together and changes rarely.
DWhen you want to enforce strict foreign key constraints.
Attempts:
2 left
💡 Hint

Think about data locality and update frequency.

query_result
intermediate
2:00remaining
Output of a $lookup join in MongoDB aggregation

Given two collections, orders and customers, what will be the output of this aggregation?

{
  $lookup: {
    from: "customers",
    localField: "customerId",
    foreignField: "_id",
    as: "customerInfo"
  }
}
MongoDB
db.orders.aggregate([
  {
    $lookup: {
      from: "customers",
      localField: "customerId",
      foreignField: "_id",
      as: "customerInfo"
    }
  }
])
AEach order document will include an array field 'customerInfo' with matching customer documents.
BThe query will return only customer documents matching orders.
CThe query will fail because $lookup requires a pipeline.
DEach customer document will include an array of orders.
Attempts:
2 left
💡 Hint

Recall what $lookup does in aggregation pipelines.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this embedding example

Which option contains a syntax error in embedding a subdocument inside a MongoDB insert?

MongoDB
db.products.insertOne({
  name: "Laptop",
  specs: {
    cpu: "Intel i7",
    ram: "16GB",
    storage: "512GB SSD"
  }
})
Adb.products.insertOne({ name: "Laptop", specs: { cpu: "Intel i7", ram: "16GB", storage: "512GB SSD" } })
Bdb.products.insertOne({ name: "Laptop", specs: [ cpu: "Intel i7", ram: "16GB", storage: "512GB SSD" ] })
C)} } "DSS BG215" :egarots ,"BG61" :mar ,"7i letnI" :upc { :sceps ,"potpaL" :eman {(enOtresni.stcudorp.bd
Db.products.insertOne({ name: "Laptop", specs: { cpu: "Intel i7", ram: "16GB", storage: "512GB SSD" } })
Attempts:
2 left
💡 Hint

Check the use of brackets for subdocuments.

optimization
advanced
2:00remaining
Optimizing read performance with embedding vs referencing

You have a blog application where each post has many comments. Which data modeling choice optimizes read performance when displaying posts with comments?

ADuplicate comments in both posts and comments collections.
BStore comments in a separate collection and reference post IDs.
CUse a relational database instead of MongoDB.
DEmbed all comments inside each post document.
Attempts:
2 left
💡 Hint

Consider how many queries are needed to read posts with comments.

🔧 Debug
expert
3:00remaining
Why does this referencing query return empty arrays?

Given collections users and orders, this aggregation returns empty arrays in the orders field. Why?

db.users.aggregate([
  {
    $lookup: {
      from: "orders",
      localField: "_id",
      foreignField: "userId",
      as: "orders"
    }
  }
])
AThe <code>userId</code> field in orders is stored as a string, but <code>_id</code> in users is an ObjectId.
BThe <code>from</code> collection name is misspelled.
CMongoDB does not support $lookup on these collections.
DThe <code>as</code> field must be a string, not an array.
Attempts:
2 left
💡 Hint

Check data types of join fields.