Mongodb aggregate (count) on multiple fields simultaneously

前端 未结 2 539
耶瑟儿~
耶瑟儿~ 2020-12-24 02:57

I\'ve got documents that look like this:

{
    \"_id\" : \"someuniqueeventid\",
    \"event\" : \"event_type_1\",
    \"date\" : ISODate(\"2014-01-14T00:00:0         


        
2条回答
  •  感动是毒
    2020-12-24 03:43

    The aggregation framework won't create keys based on data, nor should you even be doing so as "data" is not a key but actually data, so you should stick to the pattern.

    That means you can basically just do this:

    db.data.aggregate([
        { "$group": {
            "_id": {
                "event_type": "$event",
                "day": { "$dayOfWeek": "$date" }
            },
            "count": { "$sum": 1 } 
        }}
    ])
    

    And that will count the occurrences per day of week per event, albeit in multiple documents in the output, but this is easy to change to a single document per event:

    db.data.aggregate([
        { "$group": {
            "_id": {
                "event_type": "$event",
                "day": { "$dayOfWeek": "$date" }
            },
            "count": { "$sum": 1 } 
        }},
        { "$group": {
            "_id": "$_id.event_type",
            "days": { "$push": { "day": "$_id.day", "count": "$count" } }
        }}
    ])
    

    And that is in an array form, but it still holds the results you want.

    If you are really bent on doing your exact form then you want to do something like this:

    db.data.aggregate([
        { "$group": {
            "_id": "$event",
            "1": {
                "$sum": {
                    "$cond": [
                        { "$eq": [{ "$dayOfWeek": "$date" }, 1 ] },
                        1,
                        0
                    ]
                }
            },
            "2": {
                "$sum": {
                    "$cond": [
                        { "$eq": [{ "$dayOfWeek": "$date" }, 2 ] },
                        1,
                        0
                    ]
                }
            },
            "3": {
                "$sum": {
                    "$cond": [
                        { "$eq": [{ "$dayOfWeek": "$date" }, 3 ] },
                        1,
                        0
                    ]
                }
            },
            "4": {
                "$sum": {
                    "$cond": [
                        { "$eq": [{ "$dayOfWeek": "$date" }, 4 ] },
                        1,
                        0
                    ]
                }
            },
            "5": {
                "$sum": {
                    "$cond": [
                        { "$eq": [{ "$dayOfWeek": "$date" }, 5 ] },
                        1,
                        0
                    ]
                }
            },
            "6": {
                "$sum": {
                    "$cond": [
                        { "$eq": [{ "$dayOfWeek": "$date" }, 6 ] },
                        1,
                        0
                    ]
                }
            },
            "7": {
                "$sum": {
                    "$cond": [
                        { "$eq": [{ "$dayOfWeek": "$date" }, 7 ] },
                        1,
                        0
                    ]
                }
            }
        }}
    )
    

    But that is really long winded so IMHO I would stick with the first or maybe second solution as they are shorter and more easy to read.

提交回复
热议问题