EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB Projection
Secondary Sidebar
MongoDB Tutorial
  • Advanced
    • MongoDB Array
    • PostgreSQL ARRAY_AGG()
    • Indexes in MongoDB
    • MongoDB create Index
    • MongoDB Collection
    • MongoDB List Collections
    • MongoDB Capped Collections
    • MongoDB Delete Collection
    • Mongodb show collections
    • MongoDB Auto Increment
    • MongoDB Triggers
    • MongoDB Projection
    • Replication in MongoDB
    • MongoDB Database
    • Mongo DB Create Database
    • MongoDB Compass
    • MongoDB Users
    • MongoDB Authentication
    • MongoDB GridFS
    • MongoDB Relationships
    • MongoDB MapReduce
    • MongoDB Geospatial
    • MongoDB Monitoring
    • Backup in MongoDB
    • MongoDB Sharding
    • MongoDB Java Drivers
    • MongoDB Import
    • Mongo Database Interview Questions
    • MongoDB Join Two Collections
    • MongoDB Group by Multiple Fields
    • MongoDB Pagination
    • MongoDB Replica Set
    • MongoDB Bulk Update
    • MongoDB greater than
    • MongoDB Encryption
    • MongoDB find in array
    • MongoDB like query
    • Mongodb shell
    • MongoDB port
    • MongoDB Query Operators
    • MongoDB Web Interface
    • MongoDB Query Array
    • MongoDB Transactions
    • MongoDB Not In
    • MongoDB not null
    • MongoDB npm
    • MongoDB Remove
  • Basics
    • What is MongoDB
    • How To Install MongoDB
    • MongoDB Tools
    • MongoDB GUI Tools
    • MongoDB Versions
    • MongoDB Commands
    • Advantages of MongoDB
    • MongoDB Features
    • Is MongoDB NoSQL
    • Is MongoDB Open Source
    • Build Web Applications using MongoDB
    • MongoDB Data Types
    • MongoDB Administration
    • Data Modeling in MongoDB
    • MongoDB vs Elasticsearch
    • MariaDB vs MongoDB
    • Firebase vs MongoDB
  • Commands
    • Mongodb updateMany
    • MongoDB Aggregation
    • Mongodb unwind
    • Mongodb where
    • MongoDB BSON
    • MongoDB Filter
    • Mongodb Match
    • MongoDB sort by date
    • MongoDB Limit()
    • MongoDB Atlas Login
    • MongoDB Relational Database
    • MongoDB count
    • MongoDB Aggregate
    • MongoDB Distinct
    • MongoDB Unique
    • MongoDB find
    • MongoDB findOne()
    • MongoDB insert
    • MongoDB Delete
    • MongoDB Update
    • Lookup in MongoDB
    • order by in MongoDB
    • MongoDB $regex
    • MongoDB $elemMatch
    • MongoDB ObjectId()
    • MongoDB Skip()
    • MongoDB findAndModify
    • Mongodb findOneAndUpdate
    • MongoDB Date Query
    • MongoDB Timestamp
    • MongoDB sort()
    • MongoDB group by
    • MongoDB Join

Related Courses

MongoDB Certification Course

Oracle Certification Course

All in One Data Science Course

SQL Training Course

Oracle DBA Course

MS SQL Certification Course

MongoDB Projection

By Priya PedamkarPriya Pedamkar

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.

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,584 ratings)

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
Popular Course in this category
MongoDB Training Program (4 Courses, 2 Projects)
  4 Online Courses |  2 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle Training (14 Courses, 8+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
SQL Training Program (7 Courses, 8+ Projects)4.7
Oracle DBA Database Management System Training (2 Courses)4.7
MS SQL Training (16 Courses, 11+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more