Challenge - 5 Problems
Projection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Projection to include specific fields
Given a MongoDB collection users with documents containing
name, age, and email, which query will return only the name and email fields for all users?MongoDB
db.users.find({}, {name: 1, email: 1, _id: 0})Attempts:
2 left
💡 Hint
Remember to exclude the
_id field if you don't want it in the output.✗ Incorrect
Option C includes
name and email fields and excludes _id. Option C includes _id by default. Options A and D exclude or mix fields incorrectly.❓ query_result
intermediate2:00remaining
Projection excluding a field
Which MongoDB query will return all fields except the
password field from the accounts collection?MongoDB
db.accounts.find({}, {password: 0})Attempts:
2 left
💡 Hint
Use 0 to exclude a field in projection.
✗ Incorrect
Option A correctly excludes the
password field. Option A includes only password. Options C and D use invalid values for projection.📝 Syntax
advanced2:00remaining
Identify the syntax error in projection
Which option contains a syntax error in the MongoDB projection part of the query?
MongoDB
db.products.find({}, {name: 1, price: 'yes'})Attempts:
2 left
💡 Hint
Projection values must be 0 or 1, not strings.
✗ Incorrect
Option A uses a string 'yes' instead of 0 or 1, causing a syntax error. Other options use valid numeric values.
❓ query_result
advanced2:00remaining
Projection with nested fields
Given documents with a nested
address field containing city and zip, which query returns only the name and address.city fields?MongoDB
db.customers.find({}, {name: 1, 'address.city': 1, _id: 0})Attempts:
2 left
💡 Hint
Use dot notation to project nested fields.
✗ Incorrect
Option D correctly projects
name and nested address.city fields. Option D includes whole address. Option D projects wrong nested field. Option D excludes address.🧠 Conceptual
expert2:00remaining
Understanding projection behavior with _id field
In MongoDB, when you specify a projection like
{name: 1} without mentioning _id, what will be the output regarding the _id field?Attempts:
2 left
💡 Hint
Think about MongoDB's default behavior for
_id in projections.✗ Incorrect
MongoDB includes the
_id field by default unless explicitly excluded with _id: 0. So option B is correct.