开发者

Sum MongoDB Document Attributes over an Interval - Which Way?

开发者 https://www.devze.com 2023-02-07 05:23 出处:网络
I have mongo docs for every hour and they all have the same attributes.What\'s the best-practice way to sum each attribute over a timeframe?

I have mongo docs for every hour and they all have the same attributes. What's the best-practice way to sum each attribute over a timeframe?

I know I could use map-reduce, but perhaps there is a way to just do a simple group function that sums each attribute. I'm mainly wondering in ter开发者_StackOverflowms of performance.

Thanks!


just like you said, map reduce is Mongo's way for aggregation, especially if you plan to shard your data, then it's the only way for aggregation.

I really like this diagram that illustrates how to "translate" group-by query to Mongo's map-reduce.

Regarding your concern about performance:

  1. Query for the relevant records first - remember that Mongo's map-reduce usually runs on the result of a query, so make the query fast using indexes etc.
  2. The mapping and reducing is done on the mongo server, so there's no overhead on data transmission.
  3. You can improve performance using sharding. (on a single node map-reduce currently works in a single-thread. (they say they look into ways of changing this))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号