I am trying to cache a mean order to avoid retrieving all ratings and avoid aggreagtion (since the mean is to be obtained often): This is basically what I would do:
Product.findOne _id: id, (prod) ->
prod.mean = (prod.rate_count * prod.mean + rateCasted) / (prod.rating_count + 1)
prod.rate_count++
prod.save()
Problem: this implies loading the Product into the app, which I don't wanna do (performance, concurrent vote casting...)
So how do I do it with a single update
operation?
you can do it with Mongoose and keep it in model if you want. the update method updates the model and does not return it back.