我有费用明细,其中我想按月对费用进行分组,并返回该月的费用和该特定月份的费用。我什至检查了Mongoose文档是否基于月份进行汇总,但由于我保存日期如2018年1月12日,所以我的数据不符合其标准
。有什么办法可以让我获取月份和该月的总额。
我想要类似-Jan:50,Feb:45,依此类推...
以下是样本集合
{
"_id": {
"$oid": "5babee47a7d56e5fb896f181"
},
"date": "07-Sep-2018",
"expenseInfo": "Starbucks",
"category": "Restaurants",
"amount": 18
},
{
"_id": {
"$oid": "5bac361597bc8758fc065241"
},
"date": "01-Aug-2018",
"expenseInfo": "Michael kors bag",
"category": "Shopping",
"amount": 91
},
{
"_id": {
"$oid": "5baceaafc420d147806a8038"
},
"date": "14-Sep-2018",
"expenseInfo": "Gift",
"category": "Miscellaneous",
"amount": 100
}
您可以尝试$split
使用带有$arrayElemAt
索引1的运算符来返回月份值。
db.colname.aggregate(
[
{"$group":{
"_id":{"$arrayElemAt":[{"$split":["$date","-"]},1]},
"total":{"$sum":"$amount"}
}}
]
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句