This is the document I get after an aggregation query:
{
_id: <some id>,
name: 'xxxx',
sales: {
saleDate: 'dddddd',
saleValue: 9999
}
}
I need to rename the sales
field to sale
, I know I can use a projection at the end of my aggregation pipeline like this:
[
//... other aggregation stages before,
{
$project: {
sale: '$sales'
}
}
]
But the problem with that approach is that now I have to specify all the remaining fields in the projection otherwise I will get only the sale
field in the response, in this example I only have 3 fields but in my project I have much more than that.
So is there another way to rename a field in an aggregation pipeline?
Try this:
{
$set: {
sale: '$sales'
sales: '$$REMOVE'
}
}