简体   繁体   English

MongoDB更新平均值

[英]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? 那么,如何通过一次update操作呢?

you can do it with Mongoose and keep it in model if you want. 您可以使用Mongoose进行处理,并根据需要进行建模。 the update method updates the model and does not return it back. update方法将更新模型,并且不会将其返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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