0
0
MongoDBquery~20 mins

One-to-one embedding pattern in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
One-to-One Embedding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find the embedded document in a one-to-one pattern

Given a collection users where each user document embeds a single profile document, what will be the output of the following query?

{ _id: 1, name: "Alice", profile: { age: 30, city: "NY" } }

Query:

db.users.findOne({ _id: 1 }, { "profile.city": 1, _id: 0 })
MongoDB
db.users.findOne({ _id: 1 }, { "profile.city": 1, _id: 0 })
A{"profile.city": "NY"}
B{"profile": {"city": "NY"}}
C{"city": "NY"}
D{"profile": {"age": 30, "city": "NY"}}
Attempts:
2 left
💡 Hint

Projection returns the embedded document fields as nested objects.

📝 Syntax
intermediate
2:00remaining
Identify the correct syntax for embedding one-to-one documents

Which of the following MongoDB insert statements correctly embeds a one-to-one address document inside a customer document?

Adb.customers.insertOne({ _id: 101, name: "Bob" }, { address: { street: "Main St", number: 123 } })
Bdb.customers.insertOne({ _id: 101, name: "Bob" }, { $set: { address: { street: "Main St", number: 123 } } })
Cdb.customers.insertOne({ _id: 101, name: "Bob", address: [ { street: "Main St", number: 123 } ] })
Ddb.customers.insertOne({ _id: 101, name: "Bob", address: { street: "Main St", number: 123 } })
Attempts:
2 left
💡 Hint

Embedding means nesting the document inside the main document in one insert.

optimization
advanced
2:00remaining
Optimize query performance for one-to-one embedded documents

You have a products collection where each product embeds a specs document. You want to frequently query products by a field inside specs, for example specs.weight. What is the best way to optimize these queries?

ACreate an index on <code>specs.weight</code> field
BCreate a text index on the entire <code>specs</code> document
CSplit <code>specs</code> into a separate collection and use references
DUse aggregation pipeline to filter <code>specs.weight</code> after fetching all products
Attempts:
2 left
💡 Hint

Indexes speed up queries on specific fields.

🔧 Debug
advanced
2:00remaining
Debug why embedded document is missing in query result

You have documents with embedded profile in users. You run this query:

db.users.find({}, { name: 1 })

But the profile field is missing in the results. Why?

MongoDB
db.users.find({}, { name: 1 })
ABecause projection only includes <code>name</code> and excludes all other fields including <code>profile</code>
BBecause the query syntax is invalid and returns no fields
CBecause <code>profile</code> field is empty in all documents
DBecause <code>profile</code> is not embedded but referenced
Attempts:
2 left
💡 Hint

Projection controls which fields appear in results.

🧠 Conceptual
expert
3:00remaining
Why choose one-to-one embedding over referencing?

In MongoDB, when designing a schema for a one-to-one relationship, why might embedding the related document be better than using references?

AEmbedding enforces stronger data consistency by using foreign key constraints
BEmbedding allows unlimited growth of the embedded document without size limits
CEmbedding reduces the need for joins and improves read performance by storing related data together
DEmbedding automatically creates indexes on all embedded fields
Attempts:
2 left
💡 Hint

Think about how data is stored and accessed in MongoDB.