Search code examples
meteormeteor-publications

Meteor cursor counts and forEach


I need to add min and max fields into collection items in publish function and filter items by this fileds. I found the solution by using forEach for cursor:

Meteor.publish 'productsWithMinMax', (filter, options) ->
    Products.find(filter, options).forEach (p) =>
        p.min = Math.min p.price1, p.price2, p.price3, p.price4
        p.max = Math.max p.price1, p.price2, p.price3, p.price4

        if p.min && p.max && (p.max < p.mainPrice || p.min > p.mainPrice )
            @added "products", p._id, p

    Counts.publish @, 'numberOfProductsWithMinMax', Products.find(filter), {noReady: true}

    @ready()

But now Counts.publish returns wrong count for my cursor. How to count my cursor in this case?


Solution

  • The only solution i've found is to add max/min properties to collection model.

    P.S. It's will be nice if someone suggest a better one.