mongodb组在月份部分日期

问题描述:

我有以下数据。我想运行查询按类别和月份对结果进行分组并返回总计。mongodb组在月份部分日期

第一个期望的输出是月份名称的嵌套数组,其总计12个月的总计按类别。数据中不存在的月份仍将返回,但总数为0。

{"category":"Auto","month":{"Jan":9.12,"Feb":9.12,"Mar":0,...}}, 
{"category":"Fees","month":{..."Apr":0,"May":4.56,"Jun":0,...}}, 
{"category":"Travel","month":{..."Oct":0,"Nov":4.56,"Dec":0}} 

第二所需的输出是没有嵌套个月的数组...

{"category":"Auto","Jan":4.56,"Feb":4.56,"Mar":0,...}, 
{"category":"Fees",..."Apr":0,"May":4.56,"Jun":0,...}, 
{"category":"Travel",..."Oct":0,"Nov":0,"Dec":4.56,} 

如何才能将这些结果与MongoDB的查询?这里是样本输入数据:

{ 
    "_id" : ObjectId("583f6e6d14c8042dd7c153f1"), 
    "transid" : 1, 
    "category": "Auto", 
    "postdate" : ISODate("2016-01-28T05:00:00.000Z"), 
    "total" : 4.56 } 

{ 
    "_id" : ObjectId("583f6e6d14c8042dd7c153f2"), 
    "transid" : 5, 
    "category": "Auto", 
    "postdate" : ISODate("2016-01-31T05:00:00.000Z"), 
    "total" : 4.56 } 

{ 
    "_id" : ObjectId("583f6e6d14c8042dd7c153f3"), 
    "transid" : 3, 
    "category": "Auto", 
    "postdate" : ISODate("2016-02-28T05:00:00.000Z"), 
    "total" : 4.56 } 

{ 
    "_id" : ObjectId("583f6e6d14c8042dd7c153f4"), 
    "transid" : 2, 
    "category": "Auto", 
    "postdate" : ISODate("2016-02-31T05:00:00.000Z"), 
    "total" : 4.56 } 

{ 
    "_id" : ObjectId("583f6e6d14c8042dd7c153f5"), 
    "transid" : 6, 
    "category": "Fees", 
    "postdate" : ISODate("2016-05-16T05:00:00.000Z"), 
    "total" : 4.56 } 

{ 
    "_id" : ObjectId("583f6e6d14c8042dd7c153f6"), 
    "transid" : 7, 
    "category": "Travel", 
    "postdate" : ISODate("2016-11-13T05:00:00.000Z"), 
    "total" : 4.56 } 

我是新来的MongoDB和来自SQL背景,所以我觉得我已经在SQL方面一直在思考这一切。

以下是我迄今为止基于读取mongodb文档并尝试翻译“sql think”而尝试的内容。我基本上试图过滤到指定的一年(在这种情况下,2016年)。然后我按类别和日期分组。在最后一步,我计划使用项目和$ cond关键字在月份“subaggregate”,方法是指定每个月的开始日期和结束日期,然后将月份名称指定为Jan,Feb等...我有语法错误我不知道这是否正确或最好的方法。

db.transactions.aggregate(
    [ 
    { $match: { "postdate": {$gte: new Date("2016-01-01")}} }, 
    { $group: { _id: {"category":"$category","postdate":"$postdate"} , "total": { $sum: "$debit" } } }, 
    { $project: {"_id":0,"category":"$_id.category", 
     "month":{$cond: { 
          $and: 
           [ 
            { $gte: ["$_id.postdate", new Date("2016-01-01")] }, 
            { $lt: ["$_id.postdate", new Date("2016-02-01")] }, 
           ] 
          },"Jan":"$sum"} 
       //repeat for all other 11 months... 
    }} 
    ] 
) 
+0

我发现了一个解决方案,可以让我接近我期望的第一个场景的输出,但它需要进行一些调整,因为我无法在几个月内显示出来。我会继续玩下去,但它可能会让某人走上正轨。我发现这个解决方案在另一个帖子在这里:[link] http://*.com/questions/25843255/mongodb-aggregate-count-on-multiple-fields-simultaneously –

+0

这是我到目前为止:'db.transactions .aggregate {[$ match:{“postdate”:{$ gte:new Date(“2016-01-01”)}}}, {“$ group”:{ “_id”:{ “category “:”$ category“, ”month“:{”$ month“:”$ postdate“} }, ”total“:{”$ sum“:”$ total“} }},”$“组“:{ ”_id“:”$ _id.category“, ”month“:{”$ push“:{”month“:”$ month“,”total“:”$ total“}} }} ') –

如果您想按月分组,您可以使用月份操作符。例如:

db.transaction.aggregate([{$group:{_id:{ $month:"$postdate"}, "total":{$sum:1}}}]) 

我不知道是什么项目为你做的。