EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB Projection
 

MongoDB Projection

Priya Pedamkar
Article byPriya Pedamkar

Updated February 28, 2023

MongoDB Projection

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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 ()

MongoDB Projection Example 1

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:

MongoDB Projection Example 1

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:

MongoDB Projection Example 1

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:

MongoDB Projection Example 2

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:

$Meta Operator Example 3

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:

$slice Operator Example 4

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 –

  1. How Does Collection Method Works in MongoDB?
  2. MongoDB Alternatives | Top 8
  3. Lookup in MongoDB With Characteristics
  4. MongoDB Limit() | How to Works?
  5. MongoDB Unique | Examples

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW