05 / 19

What is Projection Operators?

Projection operators in MongoDB are used to control which fields are returned in query results. They allow you to include or exclude specific fields, reshape documents, and optimize performance by reducing the amount of data transferred.

Basic Projection Operators: In the .find() method, the second argument is the projection object.
  1. 1

    1 means Include.

  2. 2

    0 means Exclude.

javascript
Advanced Projection Operators:
  1. 1

    $: Projects the first element in an array that matches the query condition.

  2. 2

    $elemMatch: Projects the first element in an array that matches the specified $elemMatch condition.

  3. 3

    $meta: Projects the document's score assigned during the $text operation.

  4. 4

    $text: provides text query capabilities for self-managed (non-Atlas) deployments. For data hosted on MongoDB Atlas, MongoDB offers an improved full-text query solution, Atlas Search.

  5. 5

    $slice: Limits the number of elements projected from an array. Supports skip and limit slices.

javascript