0
0
MongoDBquery~20 mins

Projection for reducing data transfer in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Projection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Projection to include specific fields
Given a MongoDB collection users with documents containing name, age, and email, what will be the output of this query?

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

Assume the collection has:
{"name": "Alice", "age": 30, "email": "alice@example.com"}
{"name": "Bob", "age": 25, "email": "bob@example.com"}
MongoDB
db.users.find({}, {name: 1, email: 1, _id: 0})
A[{"name": "Alice", "email": "alice@example.com"}, {"name": "Bob", "email": "bob@example.com"}]
B[{"name": "Alice", "age": 30, "email": "alice@example.com"}, {"name": "Bob", "age": 25, "email": "bob@example.com"}]
C[{"age": 30, "email": "alice@example.com"}, {"age": 25, "email": "bob@example.com"}]
D[{"name": "Alice"}, {"name": "Bob"}]
Attempts:
2 left
💡 Hint
Projection includes only the fields set to 1 and excludes _id when set to 0.
query_result
intermediate
2:00remaining
Projection excluding fields
What will be the result of this MongoDB query?

db.products.find({}, {description: 0, _id: 0})

Given documents:
{"name": "Pen", "price": 1.5, "description": "Blue ink pen"}
{"name": "Notebook", "price": 3.0, "description": "200 pages"}
MongoDB
db.products.find({}, {description: 0, _id: 0})
A[{"price": 1.5}, {"price": 3.0}]
B[{"name": "Pen", "price": 1.5, "description": "Blue ink pen"}, {"name": "Notebook", "price": 3.0, "description": "200 pages"}]
C[{"name": "Pen", "price": 1.5}, {"name": "Notebook", "price": 3.0}]
D[{"name": "Pen"}, {"name": "Notebook"}]
Attempts:
2 left
💡 Hint
Setting a field to 0 excludes it from the result.
📝 Syntax
advanced
2:00remaining
Identify the invalid projection syntax
Which of the following MongoDB queries has invalid projection syntax and will cause an error?
Adb.orders.find({}, {item: true, quantity: true})
Bdb.orders.find({}, {item: 1, quantity: 1, _id: 0})
Cdb.orders.find({}, {item: 1, quantity: 0})
Ddb.orders.find({}, {item: 1, quantity: 1, _id: 1, price: 0})
Attempts:
2 left
💡 Hint
You cannot mix inclusion (1) and exclusion (0) in the same projection except for _id.
optimization
advanced
2:00remaining
Optimizing data transfer with projection
You have a large collection logs with documents containing many fields. You only need the timestamp and level fields for your report. Which query best reduces data transfer?
Adb.logs.find({}, {timestamp: 1, level: 1, _id: 0})
Bdb.logs.find({}, {timestamp: 1, level: 1})
Cdb.logs.find({}, {})
Ddb.logs.find({}, {timestamp: 0, level: 0})
Attempts:
2 left
💡 Hint
Exclude _id if you don't need it to reduce data size further.
🧠 Conceptual
expert
3:00remaining
Understanding projection behavior with nested fields
Consider documents in employees collection with nested address field:
{"name": "John", "address": {"city": "NY", "zip": "10001"}, "age": 28}

What will be the output of this query?

db.employees.find({}, {"address.city": 1, _id: 0})
MongoDB
db.employees.find({}, {"address.city": 1, _id: 0})
A[{"address": {"city": "NY", "zip": "10001"}}]
B[{"address": {"city": "NY"}}]
C[{"address.city": "NY"}]
D[{"name": "John", "address": {"city": "NY"}}]
Attempts:
2 left
💡 Hint
Projection on nested fields returns the parent object with only the specified nested fields.