Mongodb-聚合
在我的mongoDB付款托收中,我的文档结构如下:Mongodb-聚合,mongodb,mapreduce,mongoid,aggregation-framework,Mongodb,Mapreduce,Mongoid,Aggregation Framework,在我的mongoDB付款托收中,我的文档结构如下: { "created_at": created_at, "amount": amount ... } 我需要按月对记录进行分组,对每个组的金额求和,并为每个组找到增长,类似于: [{ "created_at": "2014-11-13", "amount": 10 }, { "created_at": "2014-11-3", "amount": 20 }, { "created_at": "
{
"created_at": created_at,
"amount": amount
...
}
我需要按月对记录进行分组,对每个组的金额求和,并为每个组找到增长,类似于:
[{
"created_at": "2014-11-13",
"amount": 10
},
{
"created_at": "2014-11-3",
"amount": 20
},
{
"created_at": "2014-12-13",
"amount": 30
},
{
"created_at": "2014-12-15",
"amount": 15
}]
结果:
[{
"_id": {"month": 11},
"amount": 30,
"growth": null
},
{
"_id": {"month": 12},
"amount": 45,
"growth": 1.5
}]
可以使用聚合管道、map reduce或任何其他技术吗?应该是这样的:
{
'$group' : {
'_id' : {
'month' : { '$month' : '$created_at'}
},
'amount' : { '$sum' : '$amount'}
}
}
欢迎来到SO,Artyom,如果你先做一些研究,并在你的问题中包含你到目前为止得到的东西,效果最好。这读起来更像是一个不受欢迎的工作单。增长的标准是什么?@yogesh growth[i]=amount[i]/amount[i-1]