简体   繁体   中英

MongoDB update a mean

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM