0
0
MongoDBquery~20 mins

Projection for selecting fields in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Projection Master
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, which query will return only the name and email fields for all users?
MongoDB
db.users.find({}, {name: 1, email: 1, _id: 0})
Adb.users.find({}, {name: 0, email: 0})
Bdb.users.find({}, {name: 1, email: 1})
Cdb.users.find({}, {name: 1, email: 1, _id: 0})
Ddb.users.find({}, {name: 1, email: 0})
Attempts:
2 left
💡 Hint
Remember to exclude the _id field if you don't want it in the output.
query_result
intermediate
2: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})
Adb.accounts.find({}, {password: 0})
Bdb.accounts.find({}, {password: false})
Cdb.accounts.find({}, {password: 1})
Ddb.accounts.find({}, {password: null})
Attempts:
2 left
💡 Hint
Use 0 to exclude a field in projection.
📝 Syntax
advanced
2: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'})
Adb.products.find({}, {name: 1, price: 'yes'})
Bdb.products.find({}, {name: 0, price: 0})
Cdb.products.find({}, {name: 1, price: 0})
Ddb.products.find({}, {name: 1, price: 1})
Attempts:
2 left
💡 Hint
Projection values must be 0 or 1, not strings.
query_result
advanced
2: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})
Adb.customers.find({}, {name: 1, address: 0})
Bdb.customers.find({}, {name: 1, address: 1})
Cdb.customers.find({}, {name: 1, 'address.zip': 1})
Ddb.customers.find({}, {name: 1, 'address.city': 1, _id: 0})
Attempts:
2 left
💡 Hint
Use dot notation to project nested fields.
🧠 Conceptual
expert
2: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?
AThe <code>_id</code> field is excluded by default in the output.
BThe <code>_id</code> field is included by default in the output.
CThe query will throw an error because <code>_id</code> is not specified.
DThe <code>_id</code> field will be included only if explicitly set to 1.
Attempts:
2 left
💡 Hint
Think about MongoDB's default behavior for _id in projections.