w3resource

MongoDB Aggregation Stages Operator - $project

Description

The $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.

Syntax:

{ $project: { <specifications> } }

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:

Specification Description
<field>: <1 or true> Specify the inclusion of a field.
_id: <0 or false> Specify the suppression of the _id field.
<field>: <expression> Add a new field or reset the value of an existing field.

Points to remember:

  • By default, the _id field is included in the output documents. To exclude the _id field from the output documents, you must explicitly specify the suppression of the _id field in $project.
  • A field that does not exist in the document are going to include, the $project ignores that field inclusion;
  • A new field can be added and value of an existing field can be reset by specifying the field name and set its value to some expression.

Sample collection empmast


{
        "_id" : ObjectId("553f35a0eff0e6345e7c95e7"),
        "emp_code" : "E001",
        "emp_name" : "Alex kon",
        "date_of_join" : "16/10/2010",
        "salary" : 8000
}
{
        "_id" : ObjectId("553f35d1eff0e6345e7c95e8"),
        "emp_code" : "E002",
        "emp_name" : "Peter Dex",
        "date_of_join" : "16/06/2011",
        "salary" : 6000
}
{
        "_id" : ObjectId("553f39edeff0e6345e7c95e9"),
        "emp_code" : "E003",
        "emp_name" : "Kim Nail",
        "date_of_join" : "25/12/2010",
        "salary" : 8000
}
{
        "_id" : ObjectId("553f3c20eff0e6345e7c95ea"),
        "emp_code" : "E004",
        "emp_name" : "Kim Nail",
        "date_of_join" : "16/10/2010",
        "salary" : "8000"
}

Example : $project - to include Specific Fields in Output Documents

The following command stage includes only the _id, emp_code, and the emp_name fields in its output documents:


db.empmast.aggregate( [ { $project : { emp_code : 1 , emp_name : 1 } } ] );

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 Documents

The following command excludes the _id field but includes the emp_code, and the emp_name fields in its output documents:

> db.empmast.aggregate( [ { $project : { _id: 0, emp_code : 1 , emp_name : 1 } } ] );

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 Documents

Sample collection empdetails

{
        "_id" : ObjectId("5541ffb465713ddc838b2dc7"),
        "emp_code" : "E005",
        "emp_name" : "Alan Hogg",
        "date_of_join" : "15/09/2013",
        "salary" : 9000,
        "deduction" : {
                "pf" : 2000,
                "pt" : 300,
                "it" : 200
        }
}
{
        "_id" : ObjectId("5542003c65713ddc838b2dc8"),
        "emp_code" : "E006",
        "emp_name" : "Karlos Mint",
        "date_of_join" : "23/05/2010",
        "salary" : 12000,
        "deduction" : {
                "pf" : 3000,
                "pt" : 300,
                "it" : 400
        }
}
{
        "_id" : ObjectId("554200a065713ddc838b2dc9"),
        "emp_code" : "E007",
        "emp_name" : "Burg Hence",
        "date_of_join" : "27/08/2011",
        "salary" : 10000,
        "deduction" : 4000
}

The following command includes only the pf field in the embedded document in the deduction field, you can use the dot notation:

> db.empdetails.aggregate( [ { $project: { "deduction.pf": 1 } } ] );
or 
db.empdetails.aggregate( [ { $project: { deduction: { pf: 1 } } } ] );

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 Fields

The following command adds the new fields date_of_join, PF and salary.

> db.empdetails.aggregate(
...    [
...       {
...          $project: {
...             emp_code: 1,
...             date_of_join: {
...                day: { $substr: [ "$date_of_join", 0, 2 ] },
...                month: { $substr: [ "$date_of_join", 3, 2 ] },
...                year: { $substr: [ "$date_of_join", 6, 4 ] },
...               },
...             PF: "$deduction.pf",
...             Salary: "$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
Next: $match



Follow us on Facebook and Twitter for latest update.