MongoDB monthly count empty months

时间:2018-07-24 10:21:29

标签: mongodb

I need to get count for all my orders. What I've done so far is getting count for all months in which there are orders, I need to get all months and if there are no orders count should be 0. How can I achieve that.

database.collection('orders').aggregate([
    {"$match": {"state": "finished"}},
    {
      "$project": {
         "y": {
             "$year": "$order_date"
         },
         "m": {
             "$month": "$order_date"
         }
     }
  },
  {
    "$group": {
       "_id": {
            "month": "$m",
            "year": "$y"
        },
        count: {
            "$sum": 1
        }
    }
}])

This produces following output:

[
    {
        "_id": {
            "month": 9,
            "year": 2017
        },
        "count": 2
    },
    {
        "_id": {
            "month": 8,
            "year": 2017
        },
        "count": 4
    },
    {
        "_id": {
            "month": 11,
            "year": 2017
        },
        "count": 4
    },
    {
        "_id": {
            "month": 10,
            "year": 2017
        },
        "count": 3
    },
    {
        "_id": {
            "month": 4,
            "year": 2017
        },
        "count": 2
    },
    {
        "_id": {
            "month": 6,
            "year": 2017
        },
        "count": 1
    },
    {
        "_id": {
            "month": 3,
            "year": 2017
        },
        "count": 9
    },
    {
        "_id": {
            "month": 7,
            "year": 2017
        },
        "count": 2
    },
    {
        "_id": {
            "month": 2,
            "year": 2017
        },
        "count": 3
    },
    {
        "_id": {
            "month": 12,
            "year": 2017
        },
        "count": 3
    },
    {
        "_id": {
            "month": 1,
            "year": 2017
        },
        "count": 2
    }
]

There is no results for May so I need to get count 0 for May.

1 个答案:

答案 0 :(得分:0)

我自己解决了,如果有人需要,这就是答案。

database.collection('orders').aggregate([
    {"$match": {"state": "finished"}},
    {
        "$project":
          {
            "month": { "$month": "$order_date" },
            "year": { "$year": "$order_date" }
          }
    },
    {"$group" : {
        "_id" : '$year',
        "jan" : {"$sum" : { "$cond": [ {"$eq": ["$month", 1]}, 1, 0] }},
        "feb" : {"$sum" : { "$cond": [ {"$eq": ["$month", 2]}, 1, 0] }},
        "mar" : {"$sum" : { "$cond": [ {"$eq": ["$month", 3]}, 1, 0] }},
        "apr" : {"$sum" : { "$cond": [ {"$eq": ["$month", 4]}, 1, 0] }},
        "may" : {"$sum" : { "$cond": [ {"$eq": ["$month", 5]}, 1, 0] }},
        "jun" : {"$sum" : { "$cond": [ {"$eq": ["$month", 6]}, 1, 0] }},
        "jul" : {"$sum" : { "$cond": [ {"$eq": ["$month", 7]}, 1, 0] }},
        "aug" : {"$sum" : { "$cond": [ {"$eq": ["$month", 8]}, 1, 0] }},
        "sep" : {"$sum" : { "$cond": [ {"$eq": ["$month", 9]}, 1, 0] }},
        "oct" : {"$sum" : { "$cond": [ {"$eq": ["$month", 10]}, 1, 0] }},
        "nov" : {"$sum" : { "$cond": [ {"$eq": ["$month", 11]}, 1, 0] }},
        "dec" : {"$sum" : { "$cond": [ {"$eq": ["$month", 12]}, 1, 0] }}
      }}]