I have this document:
{
"_id": "59b804e1ee8a4071a5ea3fcc",
"description" : "description",
"imagepath" : "https://example.com",
"type" : "label",
"downvotes" : -25,
"upvotes" : 15,
"language" : "en",
"approoved" : true
},
{
"_id": "59b804e1ee8a4071a5ea4dfd",
"description" : "description",
"imagepath" : "https://example.com",
"type" : "label",
"downvotes" : 0,
"upvotes" : 30,
"language" : "en",
"approoved" : true
}
How to sort these 2 objects by the sum of the upvotes
& downvotes
values?
(object with id 59b804e1ee8a4071a5ea4dfd
should come first and so on)
I've tried to aggregate with
{"$group" : {
_id:{upvotes:"$upvotes",downvotes:"$downvotes"},
count:{$sum:1}
}}
but it groups all similar documents. I can't figure out the aggregate process/syntax.
EDIT: as per Veeram answer, here's the working code using mongoose just in case someone drops here:
Labels.aggregate([
{"$addFields":{ "sort_order":{"$add":["$upvotes", "$downvotes"]}}},
{"$sort":{"sort_order":-1}},
{"$project":{"sort_order":0}}
]).exec(function (err, labels) {
if (err) {
return res.status(404).json({
error: err
});
}
res.status(200).json(labels);
});