Sum MongoDB Document Attributes over an Interval - Which Way?
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:
- 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.
- The mapping and reducing is done on the mongo server, so there's no overhead on data transmission.
- 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))
精彩评论