What is $project in mongodb?
DescriptionThe $project function in MongoDB passes along the documents with only the specified fields to the next stage in the pipeline. This may be the existing fields from the input documents or newly computed fields. Show
Syntax: { $project: { The specification for $project command contain the inclusion of fields, the suppression of the _id field, the addition of new fields, and the resetting the values of existing fields. Parameters:
Points to remember:
Sample collection empmast
Example : $project - to include Specific Fields in Output DocumentsThe following command stage includes only the _id, emp_code, and the emp_name fields in its output documents:
Output: > db.empmast.aggregate( [ { $project : { emp_code : 1 , emp_name : 1 } } ] ); { "_id" : ObjectId("553f35a0eff0e6345e7c95e7"), "emp_code" : "E001", "emp_name" : "Alex kon" } { "_id" : ObjectId("553f35d1eff0e6345e7c95e8"), "emp_code" : "E002", "emp_name" : "Peter Dex" } { "_id" : ObjectId("553f39edeff0e6345e7c95e9"), "emp_code" : "E003", "emp_name" : "Kim Nail" } { "_id" : ObjectId("553f3c20eff0e6345e7c95ea"), "emp_code" : "E004", "emp_name" : "Kim Nail" } Example : $project - to Suppress _id Field in the Output DocumentsThe following command excludes the _id field but includes the emp_code, and the emp_name fields in its output documents:
Output : > db.empmast.aggregate( [ { $project : { _id: 0, emp_code : 1 , emp_name : 1 } } ] ); { "emp_code" : "E001", "emp_name" : "Alex kon" } { "emp_code" : "E002", "emp_name" : "Peter Dex" } { "emp_code" : "E003", "emp_name" : "Kim Nail" } { "emp_code" : "E004", "emp_name" : "Kim Nail" } Example : $project - to Include Specific Fields from Embedded DocumentsSample collection empdetails
The following command includes only the pf field in the embedded document in the deduction field, you can use the dot notation:
Output: > db.empdetails.aggregate( [ { $project: { "deduction.pf": 1 } } ] ); { "_id" : ObjectId("5541ffb465713ddc838b2dc7"), "deduction" : { "pf" : 2000 } } { "_id" : ObjectId("5542003c65713ddc838b2dc8"), "deduction" : { "pf" : 3000 } } { "_id" : ObjectId("554200a065713ddc838b2dc9") } Example : $project - to Include Computed FieldsThe following command adds the new fields date_of_join, PF and salary.
Output : { "_id" : ObjectId("5541ffb465713ddc838b2dc7"), "emp_code" : "E005", "date_of_join" : { "day" : "15", "month" : "09", "year" : "2013" }, "PF" : 2000, "Salary" : 9000 } { "_id" : ObjectId("5542003c65713ddc838b2dc8"), "emp_code" : "E006", "date_of_join" : { "day" : "23", "month" : "05", "year" : "2010" }, "PF" : 3000, "Salary" : 12000 } { "_id" : ObjectId("554200a065713ddc838b2dc9"), "emp_code" : "E007", "date_of_join" : { "day" : "27", "month" : "08", "year" : "2011" }, "Salary" : 10000 } Previous: Aggregation Pipeline Operators What is difference between $Group and $project in MongoDB?$group is used to group input documents by the specified _id expression and for each distinct grouping, outputs a document. $project is used to pass along the documents with the requested fields to the next stage in the pipeline.
Why We Use unwind in MongoDB?MongoDB $unwind transforms complex documents into simpler documents, which increase readability and understanding. This also allows us to perform additional operations, like grouping and sorting on the resulting output.
What is facet in MongoDB?The $facet stage allows you to create multi-faceted aggregations which characterize data across multiple dimensions, or facets, within a single aggregation stage. Multi-faceted aggregations provide multiple filters and categorizations to guide data browsing and analysis.
What is projection MongoDB?MongoDB provides a special feature that is known as Projection. It allows you to select only the necessary data rather than selecting whole data from the document.
|