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);
});
You can try below aggregation in 3.4 for custom sort.
Use $add
to sum the upvotes and downvotes in $addFields
to keep the computed value as the extra field in the document followed by $sort
sort on field.
$project
with exclusion to drop the sort field to get expected output.
db.col.aggregate([
{"$addFields":{ "sort_order":{"$add":["$upvotes", "$downvotes"]}}},
{"$sort":{"sort_order":-1}},
{"$project":{"sort_order":0}}
])
You could create a new field in each document representing the sum of upvotes and downvotes. And then sort documents on this new field:
db.collection.aggregate([
{ $addFields: { "votes": { $sum: [ "$downvotes", "$upvotes"] } } },
{ $sort: { "votes": -1 } }
])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.