Friday, 16 February 2018

How to sort posts by upvotes AND time in MongoDB?

So I have a collection of posts where two fields are: upvotes and a timestamp.I'm trying to add another field called "points" for each post which aggregates the upvotes and timestamp.i.e. This would allow me to $sort the posts and have the ones with the highest points rise to the top.For example: if something is posted < 10 minutes ago: 10 points, < 1 hour ago: 6 points, < 4 hours ago: 4 points, 24 hours ago: 0 points. Then 1 upvote is also worth 1 point. two upvotes 2 points etc...Something posted 4 hours ago (4points) with 3 upvotes (3points) would have 7 pointsSomething posted 1 hour ago(6points) with 2 upvotes(2points) would have 8 pointsAny help much appreciated?

Submitted February 16, 2018 at 04:50PM by harrydry

No comments:

Post a Comment