आपका $temp_score और $temp_votes अभी तक आपके $divide . में मौजूद नहीं हैं ।
आप एक और $project कर सकते हैं :
db.user.aggregate([{
"$project": {
'temp_score': {
"$add": ["$total_score", 100],
},
'temp_votes': {
"$add": ["$total_votes", 20],
}
}
}, {
"$project": {
'temp_score':1,
'temp_votes':1,
'weight': {
"$divide": ["$temp_score", "$temp_votes"]
}
}
}])
या फिर से कम्प्यूटिंग temp_score और temp_votes $divide . में :
db.user.aggregate([{
"$project": {
'temp_score': {
"$add": ["$total_score", 100],
},
'temp_votes': {
"$add": ["$total_votes", 20],
},
'weight': {
"$divide": [
{ "$add": ["$total_score", 100] },
{ "$add": ["$total_votes", 20] }
]
}
}
}]);
आप इसे एक ही $project . में भी कर सकते हैं $let का इस्तेमाल करके ऑपरेटर
जिसका उपयोग 2 चर बनाने के लिए किया जाएगा temp_score और temp_votes . लेकिन परिणाम एक ही क्षेत्र के अंतर्गत पहुंच योग्य होंगे (यहां total ) :
db.user.aggregate([{
$project: {
total: {
$let: {
vars: {
temp_score: { $add: ["$total_score", 100] },
temp_votes: { $add: ["$total_votes", 20] }
},
in : {
temp_score: "$$temp_score",
temp_votes: "$$temp_votes",
weight: { $divide: ["$$temp_score", "$$temp_votes"] }
}
}
}
}
}])