0
0
MongoDBquery~10 mins

Embedding vs referencing decision in MongoDB - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to embed an address document inside a user document.

MongoDB
db.users.insertOne({ name: "Alice", address: [1] })
Drag options to blanks, or click blank then click option'
A{ street: "123 Main St", city: "Springfield" }
B"123 Main St"
C["123 Main St", "Springfield"]
DObjectId("507f1f77bcf86cd799439011")
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string or array instead of an embedded document.
Using an ObjectId which is for referencing, not embedding.
2fill in blank
medium

Complete the code to reference an address document by its ObjectId in the user document.

MongoDB
db.users.insertOne({ name: "Bob", address_id: [1] })
Drag options to blanks, or click blank then click option'
A{ street: "456 Oak St", city: "Greenville" }
BObjectId("507f1f77bcf86cd799439012")
C"456 Oak St"
D["456 Oak St", "Greenville"]
Attempts:
3 left
💡 Hint
Common Mistakes
Embedding the whole address document instead of referencing.
Using a string or array instead of an ObjectId.
3fill in blank
hard

Fix the error in the referencing code to correctly store the address reference.

MongoDB
db.users.insertOne({ name: "Carol", address_id: [1] })
Drag options to blanks, or click blank then click option'
A["507f1f77bcf86cd799439013"]
B"507f1f77bcf86cd799439013"
C{ _id: "507f1f77bcf86cd799439013" }
DObjectId("507f1f77bcf86cd799439013")
Attempts:
3 left
💡 Hint
Common Mistakes
Using a plain string instead of ObjectId().
Using an array or object instead of ObjectId.
4fill in blank
hard

Fill both blanks to embed multiple phone numbers inside a user document.

MongoDB
db.users.insertOne({ name: "Dave", phones: [1] })
// Each phone is a [2]
Drag options to blanks, or click blank then click option'
A[{ type: "home", number: "123-4567" }, { type: "work", number: "987-6543" }]
B"string"
Cobject
D["123-4567", "987-6543"]
Attempts:
3 left
💡 Hint
Common Mistakes
Using an array of strings instead of objects.
Saying each phone is a string instead of an object.
5fill in blank
hard

Fill all three blanks to create a referencing pattern with user and address collections.

MongoDB
const user = {
  name: "Eve",
  address_id: [1]
};

const address = {
  _id: [2],
  street: "789 Pine St",
  city: [3]
};
Drag options to blanks, or click blank then click option'
AObjectId("507f1f77bcf86cd799439014")
B"Springfield"
D"Greenville"
Attempts:
3 left
💡 Hint
Common Mistakes
Using strings instead of ObjectId for IDs.
Mixing up city names or using ObjectId for city.