2016-01-22

MongoDB - Group and count by two fields

db.verses.aggregate(
    {
        $group: {  
            _id: {
                FIELD_1_ALIAS : '$FIELD_1_NAME'
                , FIELD_2_ALIAS : '$FIELD_2_NAME'
            }
            , CALCULATED_FIELD_ALIAS : { $sum: 1 }
        }
    }

    // Optional stuff    , { $project: { _id:0, CALCULATED_FIELD_ALIAS:1 } }
    , { $sort: { FIELD_1_NAME:1, FIELD_2_NAME:-1 } }
    , { $limit: 15 }

);

No comments:

Post a Comment