...

Database Managment - Mongo DB

Back to Course

Lesson Description


Lession - #1418 MongoDB Projection


MongoDB Projection

MongoDB gives an extraordinary element that is known as Projection. It permits you to choose just the vital information instead of choosing entire information from the record. For instance, a report contains 5 fields, for example

{
name: "Roma",
age: 30,
branch: EEE,
department: "HR",
salary: 20000
}

Yet, we just need to show the name and the age of the worker as opposed to showing entire subtleties. Presently, here we use projection to show the name and age of the representative.
One can utilize projection with db.collection.find(>
technique. In this strategy, the subsequent boundary is the projection boundary, which is utilized to indicate which fields are returned in the matching reports.
Syntax:

db.collection.find({}, {field1: value2, field2: value2, ..}>

  • In the event that the worth of the field is set to 1 or valid, it implies the field will remember for the bring report back.
  • In the event that the worth of the field is set to 0 or misleading, it implies the field wo exclude from the bring record back.
  • You are permitted to utilize projection administrators, yet find(>
    technique doesn't uphold following projection administrators, i.e., $, $elemMatch, $slice, and $meta.
  • There is compelling reason need to set _id field to 1 to return _id field, the find(>
    strategy generally return _id except if you set a _id field to 0.

Examples: In the following examples, we are working with: Database: GeeksforGeeks Collection: employee Document: five documents that contain the details of the employees in the form of field-value pairs.


Displaying the names of the employees



Displaying the names of the employees without the _id field –



Displaying the name and the department of the employees without the _id field



Displaying the names and the department of the employees whose joining year is 2018