Is it possible to use variables defined in the $project phase in that same phase?
For example, I have this aggregation pipeline:
pipeline = [
{ '$match': {} },
{
'$group': {
'_id': '$_id',
'n': { '$first': 'n' }
}
},
{
'$project': {
'name': 1,
'n': 1,
'revenue': { '$multiply': ['$n', 2] },
'cost': { '$multiply': ['$revenue', 0.25] }
}
}
]
I'd like to use the $revenue variable (that I defined in $project) in the same $project stage to compute the value of cost, but this does not work.
Is there any way I can do this easily and efficiently? I thought about doing several projections but I would need to project many variables (~25 variables) every time I need to compute one additional variable, and I have a bunch of variables to compute that are depending on each other (~5 variables), and my code would probably look bad since there would be a lot of projecting of the same variables. How should I go about this?