0
0
MongoDBquery~20 mins

Subset pattern for large documents in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Subset Pattern Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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})
Adb.users.find({}, {name: 1, email: 1})
Bdb.users.find({}, {name: 0, email: 0})
Cdb.users.find({}, {name: 1, email: 1, _id: 0})
Ddb.users.find({}, {name: 1, email: 0, _id: 0})
Attempts:
2 left
💡 Hint
Use projection to include only the fields you want and exclude _id if not needed.
🧠 Conceptual
intermediate
1:30remaining
Why use subset pattern for large documents?
What is the main benefit of using the subset pattern when querying large documents in MongoDB?
AIt automatically indexes all fields in the document.
BIt increases the size of documents stored in the database.
CIt duplicates data to improve query speed.
DIt reduces the amount of data transferred over the network.
Attempts:
2 left
💡 Hint
Think about network efficiency and response size.
📝 Syntax
advanced
2: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})
Adb.customers.find({}, {name: 1, 'address.city': 1, _id: 0})
Bdb.customers.find({}, {name: 1, address.city: 1, _id: 0})
Cdb.customers.find({}, {name: 1, address: 1, city: 1, _id: 0})
Ddb.customers.find({}, {name: 1, address: {city: 1}, _id: 0})
Attempts:
2 left
💡 Hint
Use quotes for nested field names with dots.
optimization
advanced
2: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?
AUse projection to return only <code>title</code> and <code>summary</code> fields.
BReturn full documents and filter fields in the application code.
CCreate a new collection with only <code>title</code> and <code>summary</code> fields.
DUse aggregation pipeline without projection.
Attempts:
2 left
💡 Hint
Think about minimizing data transfer and processing.
🔧 Debug
expert
2:30remaining
Identify the error in this subset query
What error will this MongoDB query produce?

db.products.find({}, {name: 1, price: 1, quantity: 0})
ANo error, returns name and price fields only
BMongoError: Cannot mix inclusion and exclusion in projection
CSyntaxError: Unexpected token in projection
DTypeError: Invalid projection format
Attempts:
2 left
💡 Hint
In projection, you cannot mix including and excluding fields except for _id.