items = collection.aggregate([
{"$match": {}},
{"$project": {
'temp_score': {
"$add": ["$total_score", 100],
},
'temp_votes': {
"$add": ["$total_votes", 20],
},
'weight': {
"$divide": ["$temp_score", "$temp_votes"]
}
}
}
])
The total_score and total_votes have stored in the document,
I can get temp_score and temp_votes as expected, but can't get weight, any suggestion?
Your
$temp_score
and$temp_votes
are not existing yet in your$divide
.You can do another
$project
:or re-computing
temp_score
andtemp_votes
in$divide
:You can also do this in one single
$project
using the$let
operator that will be used to create 2 variablestemp_score
andtemp_votes
. But the results will be accessible under a single field (heretotal
) :