0PricingLogin
MongoDB Academy · Lesson

Inclusion vs Exclusion Projections

Learners will select or suppress specific fields in query results and understand why you cannot mix inclusion and exclusion in one projection.

What Is a Projection?

A projection is the second argument to find() or findOne() that tells MongoDB which fields to include or exclude from the result. Instead of returning the entire document, you specify exactly the fields you need. This reduces bandwidth, decreases memory usage in your application, and keeps API responses lean. Projections are one of the simplest and most impactful query optimisations available.

Inclusion Mode: Specify What to Return

In inclusion mode, you list the fields you want to receive and set their value to 1. MongoDB returns only those fields plus _id (which is included by default). This is the most common form of projection because it explicitly declares the fields your query depends on, making the intent clear to anyone reading the code.

// Return only name and email — everything else is excluded
const user = await db.collection('users').findOne(
  { email: 'alice@example.com' },
  { projection: { name: 1, email: 1 } }
);
// Result: { _id: ObjectId('...'), name: 'Alice', email: 'alice@example.com' }

All lessons in this course

  1. Inclusion vs Exclusion Projections
  2. Projecting Nested and Array Fields
  3. The $ and $elemMatch Array Projections
  4. Projection Best Practices for API Responses
← Back to MongoDB Academy