2012-04-11 147 views
2

的MongoDB数据库:地图时间戳减少

{"thread": "abc", "message": "hjhjh", "Date": (2010,4,5,0,0,0)} 
{"thread": "abc", "message": "hjhjh", "Date": (2009,3,5,0,0,0)} 
{"thread": "efg", "message": "hjhjh", "Date": (2010,3,7,0,0,0)} 
{"thread": "efg", "message": "hjhjh", "Date": (2011,4,5,0,0,0)} 

我怎么能映射简化或聚合上述数据来生成输出:

{"thread": "abc", "messages_per_month": 5} 
{"thread": "efg", "messages_per_month": 4} 

我尝试写一些代码,但在未排序的日期很难平均。是否有任何内置的函数可以平均按时完成?

回答

2

对于数字,您可以在像here这样的日期上执行最小/最大值,只需为消息添加一个计数器即可。结果将是一个包含消息数量和最小/最大日期的线程列表。从日期开始,您可以计算可用于平均消息数量的月数。

+0

谢谢!最大值,最小值使它变得容易。 – codious 2012-04-11 08:55:59