By default, a query returns every field in a matching document. A projection — the second argument to find() — lets you choose exactly which fields come back, reducing how much data crosses the network for no reason.
Set a field to 1 to include it. Once you include at least one field this way, only explicitly included fields (plus _id, unless excluded) come back:
db.users.find(
{ isActive: true },
{ name: 1, email: 1 }
)
// Returns only _id, name, and email — nothing elseSet a field to 0 to exclude it instead, keeping everything else:
db.users.find(
{},
{ passwordHash: 0 }
)
// Returns every field except passwordHash_id is the one field always included by default even in an inclusion projection — turn it off explicitly if you don't need it:
db.users.find(
{},
{ name: 1, email: 1, _id: 0 }
)Cannot mix 1 and 0 (except _id)
You cannot mix inclusion (1) and exclusion (0) in the same projection, except for _id — pick one direction per query. { name: 1, email: 0 } throws an error.
Beyond reducing network traffic, projections are the standard way to keep sensitive fields like password hashes out of results your application code sends to a browser — leaving them out at the query level is safer than remembering to strip them later.