Challenge - 5 Problems
Subset Pattern Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Find subset fields in a large document
Given a collection users where each document contains many fields, which query returns only the
name and email fields for all users?MongoDB
db.users.find({}, {name: 1, email: 1, _id: 0})Attempts:
2 left
💡 Hint
Use projection to include only the fields you want and exclude _id if not needed.
✗ Incorrect
Option C correctly includes only 'name' and 'email' fields and excludes '_id'. Option C includes '_id' by default. Option C excludes 'name' and 'email' which is opposite of the goal. Option C mixes inclusion and exclusion which is invalid.
🧠 Conceptual
intermediate1:30remaining
Why use subset pattern for large documents?
What is the main benefit of using the subset pattern when querying large documents in MongoDB?
Attempts:
2 left
💡 Hint
Think about network efficiency and response size.
✗ Incorrect
Using subset pattern returns only needed fields, reducing data sent over the network and improving performance. Other options are incorrect or unrelated.
📝 Syntax
advanced2:30remaining
Correct projection syntax for nested fields
Which query correctly returns only the
name and the nested field address.city from documents in the customers collection?MongoDB
db.customers.find({}, {name: 1, 'address.city': 1, _id: 0})Attempts:
2 left
💡 Hint
Use quotes for nested field names with dots.
✗ Incorrect
Option A uses correct syntax with quotes around 'address.city'. Option A is invalid syntax. Option A tries to nest projection which is not supported. Option A incorrectly projects 'city' as a top-level field.
❓ optimization
advanced2:00remaining
Optimizing queries with subset pattern
You have a collection with large documents. You only need to display the
title and summary fields in your app. Which approach optimizes query performance?Attempts:
2 left
💡 Hint
Think about minimizing data transfer and processing.
✗ Incorrect
Using projection reduces data sent and speeds up queries. Filtering in app wastes bandwidth. Creating new collection duplicates data and adds maintenance. Aggregation without projection returns full documents.
🔧 Debug
expert2:30remaining
Identify the error in this subset query
What error will this MongoDB query produce?
db.products.find({}, {name: 1, price: 1, quantity: 0})Attempts:
2 left
💡 Hint
In projection, you cannot mix including and excluding fields except for _id.
✗ Incorrect
MongoDB projection rules forbid mixing inclusion (1) and exclusion (0) except for _id. Here, 'quantity: 0' conflicts with 'name: 1' and 'price: 1', causing an error.