Problem Statement
In MongoDB projections, what does { name: 1, age: 1, _id: 0 } do?
Explanation
This projection returns only the name and age fields while explicitly excluding the underscore id field. In MongoDB projections, 1 means include the field and 0 means exclude it.
By default, the underscore id field is always included unless you explicitly exclude it with underscore id colon 0. You cannot mix inclusion and exclusion in projections, except for underscore id. Projections improve performance by reducing the amount of data transferred over the network.
Code Solution
SolutionRead Only
// Return only name and age, exclude _id
db.users.find(
{ age: { $gt: 25 } },
{ name: 1, age: 1, _id: 0 }
)
// Returns:
{ "name": "Alice", "age": 30 }
{ "name": "Bob", "age": 28 }