Aggregation task - $project
A scope aggregation task that enables the selection of fields. You can also assign new output field names and apply functions to field values.
You can define projected fields as source field extraction with or without assigned output names. And, projected fields can also be derived fields from calculations. Fields can be included only, not excluded.
The projected fields of a task must be referenced in the next task with the assigned target names.
Example
Example 1
db.furniture.aggregate({"$project":{"price":1, color:1}})
db.furniture.aggregate({"$project":{"aprice":"$price", "mycolor": "$color"}})
Example 2
db.furniture.aggregate({"$project":{"priceTemp": {"$add":["$price","$txcount"]},"color":1}}),
{"$project":{"priceMulti":{"$multiply":["$price","$priceTemp]},"acolor":"$color"}}),
{"$project":{"priceAndCountXPriceDiv2":{"$divide":[$priceMulti,2]},"acolor":1}})
The same query in a single task:
db.furniture.aggregate({"$project":{"priceAndCountXPriceDiv2":{"$divide":[{"$multiply":["$price",{"$add":["$price","$txcount"]}]},2]},"color":1}})