Projection
Control exactly which fields a query returns using projection, reducing network and memory overhead.
What Is Projection?
Projection is the second argument to find(), controlling which fields are included in the returned documents — useful for reducing data transferred over the network when you only need a few fields from a much larger document.
Including Specific Fields
Setting a field to 1 includes only that field (plus _id, which is included by default).
db.users.find({}, { name: 1, email: 1 });// Returns only _id, name, and email for every documentExcluding Specific Fields
Setting a field to 0 excludes just that field, keeping everything else.
db.users.find({}, { password: 0 });// Returns every field except passwordA projection must be either all inclusion or all exclusion — the only exception is _id, which can be explicitly excluded (_id: 0) alongside an otherwise inclusive projection.
Projecting Nested and Array Fields
db.users.find({}, { "address.city": 1 }); // only the nested city fielddb.posts.find({}, { comments: { $slice: 3 } }); // only the first 3 comments in the arrayCommon Beginner Mistakes
MongoDB rejects { name: 1, email: 0 } as invalid — pick either inclusion or exclusion for a given projection.
A field like passwordHash is included unless explicitly excluded — always project it out (or use a dedicated view/API layer) before sending data to a client.
FAQs
Mainly the latter — it reduces network transfer and memory usage for the result set, though in some cases it can also allow a query to be satisfied entirely from an index (a "covered query").
Closely related, but $project (covered in the aggregation lessons) is more powerful — it can also compute new fields, not just include/exclude existing ones.
Summary
Projection trims down exactly which fields a query returns, reducing unnecessary data transfer. With core reading fundamentals covered, the next section moves into updating and deleting data.