Projection Operators

Projection operators in MongoDB are used in the queries to control the fields that should be included or excluded from the result set. They can either limit the fields to be returned or specify the fields to be excluded from the results. In this section, we will look at some common projection operators available in MongoDB, such as $, $elemMatch, and $slice.

1. $

The $ operator is used to project the first element in an array that matches the specified condition. It is especially useful when dealing with large arrays, and you only need the first element matching a given condition.


{ <field>: { $elemMatch: { <query1>, <query2>, ... } } }

Usage example:

db.collection.find({ grades: { $gte: 80 } }, { name: 1, 'grades.$': 1 });

This will return only the first grades element greater than or equal to 80 along with the name field.

2. $elemMatch

The $elemMatch operator matches documents in a collection that contain an array field with at least one element that satisfies multiple given conditions.


{ <field>: { $elemMatch: { <query1>, <query2>, ... } } }

Usage example:

  subjects: { $elemMatch: { score: { $gte: 80 }, type: 'exam' } },

This will return documents that have at least one subjects element with a score greater than or equal to 80 and a type of “exam”.

3. $slice

The $slice operator is used to limit the number of elements projected from an array. It can either return the first N elements, skip the first N elements, or return elements after skipping N elements.


{ <field>: { $slice: <num_elements> } }


{ <field>: { $slice: [ <skip_count>, <num_elements> ] } }

Usage example:

db.collection.find({}, { name: 1, grades: { $slice: 3 } });

This will return the name field and the first 3 grades elements for all documents in the collection.

db.collection.find({}, { name: 1, grades: { $slice: [1, 2] } });

This will return the name field and the 2 grades elements after skipping the first element for all documents in the collection.

In summary, projection operators play a crucial role in retrieving specific data from MongoDB collections as they allow you to get the desired output. Using the appropriate operator for your query can help optimize the performance and efficiency of your MongoDB queries.