I have this product set and I want to display top 10 products based on a custom sort function
[{ _id: 1, title, tags:['a'], createdAt:ISODate("2016-01-28T00:00:00Z") } , { _id: 2, title, tags:['d','a','e'], createdAt:ISODate("2016-01-24T00:00:00Z") }]
What I want to do is sort it based on a โmagic scoreโ that can be calculated. For example, based on this formula: tag_count*5 - number_of_days_since_it_was_created .
If the first is 1 day, this makes an estimate:
[{_id:1 , score: 4}, {_id:2, score: 10}]
I have a few ideas on how I can achieve this, but I'm not sure how good they are, especially since I'm new to mongo and meteor:
start the observer (Meteor.observe) and each time the document is changed (or a new one is created), recount the account and update it on the collection itself. If I do this, I can simply use $ orderBy where I need it.
after some reading, I found that mongo aggregate or map_reduce can help me achieve the same result, but as far as I know, the meteor does not support it directly
sort the collection on the client side as an array, but using this method I'm not sure how it will behave by page (given that I subscribe to a limited number of documents)
Thanks for any information you can share with me!
source share