Custom for meteor mongodb collection

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!

+6
source share
1 answer

Literal sorting of functions is only done in meteor , so you should be able to do something like

 Products.find({}, {sort: scoreComparator}); 

in the upcoming release .

When creating a collection, you can use the transform property. In this transformation, save the magic operation as a function.

 score=function(){ // return some score }; transformer=function(product){ product.score=score; // one could also use prototypal inheritance }; Products=new Meteor.Collection('products',{transform:transformer}); 

Unfortunately, you cannot use the sort operator in virtual fields yet, because minimongo does not support it.

Thus, the final fall back, as you mentioned, or sorting a virtual field or sorting a competent function are supported in minimongo, this is client-side sorting:

 // Later, within some template scoreComparator=function(prd1,prd2){ return prd1.score()-prd2.score(); } Template.myTemplate.helpers({ products:function(){ return Products.find().fetch().sort(scoreComparator); } }); 

I'm not sure how it will look paginated (given that I subscribe to a limited number of documents)

EDIT : The score will be counted among the signed documents.

+7
source

All Articles