0
0
MongoDBquery~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB One-to-Many Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find all authors with their books embedded
Given a MongoDB collection authors where each document embeds an array of books, what is the output of this query?

db.authors.find({}, {name: 1, books: 1, _id: 0})
MongoDB
db.authors.find({}, {name: 1, books: 1, _id: 0})
A[ { _id: ObjectId("...") }, { _id: ObjectId("...") } ]
B[ { name: "Alice" }, { name: "Bob" } ]
C[ { books: [{title: "Book A1"}, {title: "Book A2"}] }, { books: [{title: "Book B1"}] } ]
D[ { name: "Alice", books: [{title: "Book A1"}, {title: "Book A2"}] }, { name: "Bob", books: [{title: "Book B1"}] } ]
Attempts:
2 left
💡 Hint
Projection includes name and books, excludes _id.
🧠 Conceptual
intermediate
1:30remaining
Why embed one-to-many data in MongoDB?
Which is the main reason to use embedding for one-to-many relationships in MongoDB?
ATo allow multiple collections to share the same document
BTo enforce strict relational integrity like foreign keys
CTo keep related data together for faster reads and simpler queries
DTo reduce document size below 1 KB
Attempts:
2 left
💡 Hint
Think about how embedding affects data retrieval speed.
📝 Syntax
advanced
2:00remaining
Correct syntax to add a book to an author's embedded books array
Which MongoDB update command correctly adds a new book to the embedded books array of the author named "Alice"?
MongoDB
New book to add: { title: "Book A3", year: 2024 }
Adb.authors.updateOne({name: "Alice"}, {$addToSet: {books: {title: "Book A3", year: 2024}}})
Bdb.authors.updateOne({name: "Alice"}, {$push: {books: {title: "Book A3", year: 2024}}})
Cdb.authors.updateOne({name: "Alice"}, {$set: {books: {title: "Book A3", year: 2024}}})
Ddb.authors.updateOne({name: "Alice"}, {$push: {books: [{title: "Book A3", year: 2024}]}})
Attempts:
2 left
💡 Hint
Use the operator that adds an element to an array.
🔧 Debug
advanced
2:30remaining
Why does this embedded update fail?
Given this update command:

db.authors.updateOne({name: "Alice"}, {$push: {books: {title: "Book A4"}}})

It fails with an error about document size. What is the most likely cause?
AThe embedded books array is too large and exceeds MongoDB's 16MB document size limit
BThe $push operator requires an array, not an object
CThe query filter {name: "Alice"} does not match any document
DThe update command is missing the <code>multi: true</code> option
Attempts:
2 left
💡 Hint
Think about MongoDB document size limits when embedding many items.
optimization
expert
3:00remaining
Best approach to model a one-to-many relationship with thousands of related items
You have an author with thousands of books. Embedding all books in one document causes performance and size issues. What is the best MongoDB design pattern to handle this?
AUse referencing: store author in one collection and books in another with author_id field
BSplit the books array into multiple embedded arrays inside the author document
CEmbed all books anyway and increase MongoDB document size limit
DStore all books as a single large string field inside the author document
Attempts:
2 left
💡 Hint
Consider MongoDB document size limits and query performance.