Projection: choosing fields
find()projection
A projection is the second argument to find(), and it decides which fields come back. Two rules cover almost everything: you cannot mix inclusion and exclusion in one projection, and _id is included unless you explicitly turn it off.
Suppose the document contains:
{
_id: 1,
name: "Denish",
email: "denish@example.com",
passwordHash: "secret",
address: {...}
}
You probably don’t want everything.
db.users.find(
{
status: "active"
},
{
name: 1,
email: 1
}
)
Result:
{
_id: 1,
name: "Denish",
email: "denish@example.com"
}
_id is included by default.
Exclude it:
db.users.find(
{},
{
_id: 0,
name: 1,
email: 1
}
)
Exclusion projection
db.users.find(
{},
{
passwordHash: 0,
internalNotes: 0
}
)
Generally, you don’t mix inclusion and exclusion in the same projection, except _id.