I am curious whether one can use a computed field in a $project stage, to calculate another computed field.
For example using db.solarSystem in the aggregation DB, can we do an aggregation, with a project stage, where we create two computed fields (one calculated based on the other), like below?
db.solarSystem.aggregate([
{
$project:{
"_id": 0,
"name": 1,
"computed_field_01": {
$divide: [ "$gravity.value", 5 ]
},
"computed_field_02": {
$multiply : [ "$computed_field_01", 10 ]}
}
}
])
or do we always have to break into two stages, like this:
db.solarSystem.aggregate([
{
$project:{
"_id": 0,
"name": 1,
"computed_field_01": {
$divide: [ "$gravity.value", 5 ]
}
}
},
{
$project:{
"_id": 0,
"name": 1,
"computed_field_01":1,
"computed_field_02": {
$multiply : [ "$computed_field_01", 10 ]}
}
}
])
Also just let me mention that I have tried a few syntax variations on the “combined” stage but to no avail.
Thank you in advance for your help.