MongoDb如何按月和年汇总

时间:2017-08-02 08:10:40

标签: mongodb aggregate

我是mongodb的新手并且正在尝试学习Mongodb查询

    {
    "_id" : ObjectId("59815d4704ca1760a45957ca"),
    "userEmail" : "lk@gmail.com",
    "expenseAmount" : 200,
    "expenseRemark" : "aa",
    "expenseCategory" : "billing",
    "entryTime" : ISODate("2017-08-02T05:03:57Z"),
    "__v" : 0
}
{
    "_id" : ObjectId("59815d5404ca1760a45957cb"),
    "userEmail" : "lk@gmail.com",
    "expenseAmount" : 300,
    "expenseRemark" : "ff",
    "expenseCategory" : "transport",
    "entryTime" : ISODate("2017-08-02T05:04:11Z"),
    "__v" : 0
}
{
    "_id" : ObjectId("5980191d04ca1760a45957cd"),
    "userEmail" : "lk@gmail.com",
    "expenseAmount" : 100,
    "expenseRemark" : "rr",
    "expenseCategory" : "billing",
    "entryTime" : ISODate("2017-08-01T06:00:46Z"),
    "__v" : 0
}
{
    "_id" : ObjectId("5980192604ca1760a45957ce"),
    "userEmail" : "lk@gmail.com",
    "expenseAmount" : 200,
    "expenseRemark" : "qq",
    "expenseCategory" : "transport",
    "entryTime" : ISODate("2017-08-01T06:01:03Z"),
    "__v" : 0
}
{
    "_id" : ObjectId("5980192e04ca1760a45957cf"),
    "userEmail" : "lk@gmail.com",
    "expenseAmount" : 470,
    "expenseRemark" : "ff",
    "expenseCategory" : "transport",
    "entryTime" : ISODate("2017-08-01T06:01:11Z"),
    "__v" : 0
}
{
    "_id" : ObjectId("59816ac004ca1760a45957d0"),
    "userEmail" : "lk@gmail.com",
    "expenseAmount" : 500,
    "expenseRemark" : "raj",
    "expenseCategory" : "transport",
    "entryTime" : ISODate("2017-08-02T06:01:26Z"),
    "__v" : 0
}
{
    "_id" : ObjectId("59816acb04ca1760a45957d1"),
    "userEmail" : "lk@gmail.com",
    "expenseAmount" : 100,
    "expenseRemark" : "pet",
    "expenseCategory" : "pets",
    "entryTime" : ISODate("2017-08-02T06:01:37Z"),
    "__v" : 0
}
{
    "_id" : ObjectId("597d7a9c04ca1760a45957d2"),
    "userEmail" : "lk@gmail.com",
    "expenseAmount" : 500,
    "expenseRemark" : "gt",
    "expenseCategory" : "sports",
    "entryTime" : ISODate("2017-07-30T06:20:04Z"),
    "__v" : 0
}
{
    "_id" : ObjectId("597d7aaa04ca1760a45957d3"),
    "userEmail" : "lk@gmail.com",
    "expenseAmount" : 560,
    "expenseRemark" : "mov",
    "expenseCategory" : "entertainment",
    "entryTime" : ISODate("2017-07-30T06:20:14Z"),
    "__v" : 0
}

我想将expenseAmount按年+月分组。 我试过了

`db.expenses.aggregate( 
       {$project : { 
              month : {$month : "$entryTime"}, 
              year : {$year :  "$entryTime"}
          }}, 
        {$group : { 
                _id : {month : "$month" ,year : "$year" },  
              total : {$sum : "$expenseAmount"} 
        }})`

哪个给出了

{ "_id" : { "month" : 7, "year" : 2017 }, "total" : 0 }

{ "_id" : { "month" : 8, "year" : 2017 }, "total" : 0 }

请指导我如何获得汇总结果。 我无法弄清楚这样做的方法。

由于

3 个答案:

答案 0 :(得分:7)

您在投影阶段丢失了new FileWriter(FILENAME2,true)字段。只需添加它:

expenseAmount

请注意,如果文档中不存在字段,则$sum将返回0.

另请注意,还有另一个聚合运算符,它按预期执行 - $addFields。它添加了新字段以记录和保留输入文档中的所有现有字段。但在这种情况下,您只需要expenseAmount字段

答案 1 :(得分:0)

  db.expenses.aggregate( 
    {$group : { 
            _id : { 
          month : {$month : "$entryTime"}, 
          year : {$year :  "$entryTime"}
      }},  
          total : {$sum : "$expenseAmount"} 
    }})

无需添加“项目”,只有组才能完成所有工作。

答案 2 :(得分:0)

在这种情况下,不需要$ project。只​​需使用以下内容,您将获得预期的结果。

 db.expenses.aggregate( 
   {$group : { 
           _id : { 
         month : {$month : "$entryTime"}, 
         year : {$year :  "$entryTime"}
     }},  
         total : {$sum : "$expenseAmount"} 
   }})