project
$project
Control the fields you want to include or not in the documents you pass to the next stage in the pipeline.
You can project with the following objectives:
- Include only specific fields
- Exclude specific fields
- Create a new field that has a computed value
Note: If you exclude fields, you cannot also specify fields to include.
Syntax
{ $project: { specification(s) } }
For more information, see the following specification examples:
| Example | Result |
|---|---|
{ $project: { field1: 1 } } | Only field1 and its value project to the next stage in the pipeline. |
{ $project: { field1: 0 } } | All fields except field1 project to the next stage in the pipeline. |
{ $project: { newField: { $add: ["field1", "field2"} } | Projects a newly created field, newField, with a computed value to the next stage in the pipeline. |
Sample Request
[
{
"$match": {
"temp": {
"$exists": true
}
}
},
{
"$project": {
"temp": 1,
"_ts": 1
}
},
{
"$sort": {
"_ts": 1
}
},
{
"$limit": 3
}
]
Sample Response
{
"_list": [
{
"temp": 368,
"_id": "645bca63f8b8a87c0f075ce9",
"_ts": "Wed May 10 22:16:26 IST 2023"
},
{
"temp": 871,
"_id": "645bca63f8b8a87c0f075cea",
"_ts": "Wed May 10 22:16:26 IST 2023"
},
{
"temp": 777,
"_id": "645bca63f8b8a87c0f075ceb",
"_ts": "Wed May 10 22:16:27 IST 2023"
}
]
}