I want to show a list of posts from the database based on likes and date, think of the basic "trending" items page.
I want to use a formula like score = likes / daysSinceCreation and then get the first 10 posts based on this score.
How can I add that sort function with mongoDB/Mongoose?
Posts.find().sort(???).limit(10).then(posts => console.log(posts));
Currently I can get top posts in last week (find if creation date larger than last week and order by score), but how can I implement a more complex sorting function without getting all the items from the DB?
eg: Today is Friday
ID CREATION_DAY LIKES
1 Monday 4 // score is 5/5 = 0
2 Tuesday 10 // score is 10/4 = 2
3 Wednesday 3 // score is 3/3 = 1
4 Thursday 20 // score is 20/2 = 10
5 Friday 5 // score is 5/1 = 5
Sorted list of IDs is: [4 (Th), 5 (Fr), 2 (Tu), 3 (We), 1(Mo)]
This will create a new document in a "trendingposts" table:
A few things to note:
If using Mongo 3.4+ the $project stage can also be written as:
{ $min: now }is just a hack to grab the minimum value ofnowon each document, even though it's the same value for all of them."$$ROOT"is the entire current document. This means your end result will be a single object with the form:You can then query with:
If your description/title are changing frequently, instead of
$pushing the entire document in, you could just push the ids and use them for an$inquery on your posts in order to guarantee the latest data.