2

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?

1 Answer 1

4

Try this:

{
  $set: {
    sale: '$sales'
    sales: '$$REMOVE'
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.