Challenge - 5 Problems
MongoDB One-to-Many Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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})Attempts:
2 left
💡 Hint
Projection includes
name and books, excludes _id.✗ Incorrect
The query returns all authors with only their
name and embedded books fields, excluding the _id field.🧠 Conceptual
intermediate1:30remaining
Why embed one-to-many data in MongoDB?
Which is the main reason to use embedding for one-to-many relationships in MongoDB?
Attempts:
2 left
💡 Hint
Think about how embedding affects data retrieval speed.
✗ Incorrect
Embedding keeps related data in one document, which makes reading related data faster and queries simpler.
📝 Syntax
advanced2: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 }Attempts:
2 left
💡 Hint
Use the operator that adds an element to an array.
✗ Incorrect
The $push operator adds a new element to an array field. Option B uses correct syntax.
🔧 Debug
advanced2:30remaining
Why does this embedded update fail?
Given this update command:
It fails with an error about document size. What is the most likely cause?
db.authors.updateOne({name: "Alice"}, {$push: {books: {title: "Book A4"}}})It fails with an error about document size. What is the most likely cause?
Attempts:
2 left
💡 Hint
Think about MongoDB document size limits when embedding many items.
✗ Incorrect
MongoDB documents have a 16MB size limit. Embedding too many books can exceed this limit causing update failure.
❓ optimization
expert3: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?
Attempts:
2 left
💡 Hint
Consider MongoDB document size limits and query performance.
✗ Incorrect
Referencing stores related data in separate collections, avoiding document size limits and improving performance for large one-to-many relationships.