Updated February 28, 2023
Introduction to MongoDB Projection
MongoDB projection is used to select the specific data (which was necessary) instead of selecting all the data from the document. If our document contains 10 fields and we need to show only 5 fields at that time, we have used MongoDB projection operators. Projection is most important to select necessary data from whole documents, Mainly in MongoDB $, $elemMatch, $slice, and $Meta projection operators are used. We can improve the database server’s performance by using projection, and database performance is increased after using projection.
Operators in MongoDB Projection
Below are the operators in MongoDB Projection:
1. We can select specific data from a document by using the projection operator. Below is the projection operator available in MongoDB.
- $
- $elemMatch
- $slice
- $Meta
2. MongoDB projection operator will increase the performance of the database server. We can get the specific data from documents, so a database server’s overhead is reduced using a projection operator.
3. We have using the student_data table to describe the example of a projection operator. Below is the data of student_data table are as follows.
db.student_data.find ()
The above example shows the student_data table to describe the example of a projection operator in MongoDB.
1. $
- The $ projection operator is used to limit the content of an array from which a query of the result will match only the first elements of the documents.
- We can use a $ projection operator with find () method and findone (). When we require a single element of an array, we have used a $ projection operator.
Below is the syntax of the $ projection operator.
Syntax:
collection_name.find (Find method used with projection operator)({<array> :<value> ...}, {"<array>.$" ($ operator is used with array) : 1})
collection_name.find (Find method used with projection operator) ({<array.feildname> : <value> ...}, {"<array>.$" ($ operator is used with array) : 1})
Below is the parameter Description of above Syntax:
- Find (): Find method is used with $ projection operator.
- $: The $ projection operator is used to limit the content of an array in MongoDB.
- Collection name: The collection name was used to show the result using a $ projection operator in MongoDB.
- Array: Array of field name using projection operator.
- Field: This is a state that field name of MongoDB documents.
Example 1: The below example shows project array values using a $ projection operator.
Code:
db.student_data.find( { semester: 3, grades: { $gte: 75 } },{ "grades.$": 1 } )
Output:
The above example will return a student’s grades which were greater than 75.
Example 2: The below example shows a student grade which was less than 75.
Code:
db.student_data.find( { semester: 3, grades: { $lte: 75 } },{ "grades.$": 1 } )
Output:
2. $elemMatch
- The $elemMatch projection operator is used to limit the content of an array from which query of the result will match only the first elements of the documents.
- The $ and $elemMatch operator match first matching element of an array based on condition.
Syntax:
collection_name.find (Find method used with projection operator) ({field_name : <value> ...}, {"Field_name.$elemMatch{}" ($elemMatch operator) })
Below is the parameter description of the above syntax:
- Find (): Find method is used with $elemMatch projection operator.
- Collection name: Collection name which was used to show the result using a $elemMatch projection operator.
- Field: This is a state that field name of MongoDB documents.
- $elemMatch: It is used to limit the content of the array from the query.
Example: Example shows $elemMatch projection operator.
Code:
db.student_data.find( { semester: 3 },{ stud_id: { $elemMatch: { semester: 3 } } } )
Output:
3. $Meta
- The Metadata projection operator in MongoDB is used to return each matching document associated with the query.
- The Meta projection operator is used with “textscore” keyword. The sort order of “textscore” keyword is descending.
- TextScore keyword returns the corresponding $text descending query for each document.
Syntax:
collection_name.aggregate (Aggregate method used with projection operator) (<query> (Query used to process the operation) , { Score : { meta: “textscore” (textscore keyword used with meta projection operator)) } )
{ $meta: <metadataKeyword> (textscore keyword used) }
Below is the parameter description of the above syntax:
- Aggregate (): Aggregate method is used with $Meta projection operator in MongoDB.
- Collection name: The collection name was used to show the result using a $Meta projection operator in MongoDB.
- Query: Query was used to process the operation using a $Meta projection operator.
- $Meta: The Metadata projection operator is used to return each matching document.
- Textscore: Text score keyword is used with $Meta projection operator.
Example: Example of $Meta projection operator in MongoDB.
Code:
db.student_data.aggregate([{ $group: { _id: { $meta: "textScore" }, count: { $sum: 3 } } } ])
db.student_data.aggregate([{ $group: { _id: { $meta: "textScore" }, count: { $sum: 5 } } } ])
Output:
4. $slice
- $Slice operator is used to control the number of an array that query returns. MongoDB views are not supporting the slice operation.
- $Slice is handy and important for projection operators.
Syntax:
collection.find (Find method used with projection operator) ( { field (Field name of collection): value }, { array: {$slice: count } } );
Example: Below is the example of the $slice projection operator.
Code:
db.student_data.find ( {}, { comments: { $slice: [ 20, 30 ] } } )
Output:
Conclusion
MongoDB projection is used to select the specific data (which was necessary) instead of selecting all the data from the document. In MongoDB $, $elemMatch, $slice and $Meta projection operators are available. Projection operator is handy and important in MongoDB to find user-specific data from all the data.
Recommended Article
This is a guide to MongoDB Projection. Here we discuss the Introduction to MongoDB Projection and its different operators along with its examples. You can also go through our other suggested articles to learn more –