MongoDB通过不同列的总和聚合组

时间:2015-10-19 16:26:08

标签: mongodb mongodb-query aggregation-framework

我有以下示例数据的分析集合。

{ "_id" : ObjectId("55f996a4e4b0cc9c0a392594"), "action" : "apiUploadFile", "assetId" : "55f996a4e4b0cc9c0a392593" },
{ "_id" : ObjectId("5603d384e4b0cf75af10be88"), "action" : "agAsset",  "assetId" : "55f996a4e4b0cc9c0a392593"},
{ "_id" : ObjectId("5603d395e4b0cf75af10becc"), "action" : "aAD", "assetId" : "55f996a4e4b0cc9c0a392593" },
{ "_id" : ObjectId("5603d395e4b0cf75af10becd"), "action" : "mobCmd", "assetId" : "55f996a4e4b0cc9c0a392593", sessionId : "123"},
{ "_id" : ObjectId("5603d395e4b0cf75af10bece"), "action" : "mobCmd", "assetId" : "55f996a4e4b0cc9c0a392593", sessionId : "1234" },
{ "_id" : ObjectId("5603d395e4b0cf75af10becf"), "action" : "mobCmd", "assetId" : "55f996a4e4b0cc9c0a392593", sessionId : "1234" }

我需要通过" assetId'找到分析组的总和。然后对于每个动作'类型。我想出了以下查询

db.analytics.aggregate(
    [
    {
        $match : { 
            'assetId' : { "$ne": null }
        }
    },
    {$group :{
            _id:
            {
                assId:'$assetId'
            },
            viewCount:{
                $sum:{
                    $cond: [ { $eq: [ '$action', 'agAsset' ] }, 1, 0 ]
                }
            },
            sessionCount:{
                $sum:{
                    $cond: [ { $eq: [ '$action', 'mobCmd' ] }, 1, 0 ]
                }
            }
        }
    }]
    )

这很有效,除了我找不到' sessionCount'使用distinct' sessionId'。例如,这是当前输出

{ "_id" : { "assId" : "55f996a4e4b0cc9c0a392593" }, "viewCount" : 1,  "sessionCount" : 3 }

预期输出

{ "_id" : { "assId" : "55f996a4e4b0cc9c0a392593" }, "viewCount" : 1,  "sessionCount" : 2 }

我需要找到action='mobCmd'的sessionCount,distinct的值为sessionId。如何在' sessionCount'中使用不同的内部$ sum运算?部?

1 个答案:

答案 0 :(得分:1)

您需要在复合_id字段中对文档进行分组。

db.collection.aggregate([
    { "$match": { "assetId": { "$ne": null }}},
    { "$group": { 
        "_id": { "assId": "$assetId", "sessionId": "$sessionId" }, 
        "viewCount": { 
            "$sum": { 
                "$cond": [ 
                    { "$eq": [ "$action", "agAsset" ] }, 
                    1,
                    0 
                ] 
            }
         }, 
         "sessionCount": { 
             "$sum": { 
                 "$cond": [ 
                     { "$eq": [ "$action", "mobCmd" ] }, 
                     1, 
                     0 
                 ] 
             }
         }
     }}
])

哪个收益率:

{ "_id" : { "assId" : "55f996a4e4b0cc9c0a392593", "sessionId" : "1234" }, "viewCount" : 0, "sessionCount" : 2 }
{ "_id" : { "assId" : "55f996a4e4b0cc9c0a392593", "sessionId" : "123" }, "viewCount" : 0, "sessionCount" : 1 }
{ "_id" : { "assId" : "55f996a4e4b0cc9c0a392593" }, "viewCount" : 1, "sessionCount" : 0 }

或者使用$addToSet运算符返回数组中唯一sessionId$unwind的数组,然后重新group您的文档。

db.collection.aggregate([
    { "$match": { "assetId": { "$ne": null }}}, 
    { "$group": { 
        "_id": "$assetId", 
        "sessionId": { "$addToSet": "$sessionId" }, 
        "viewCount": { 
            "$sum": { 
                "$cond": [ 
                    { "$eq": [ "$action", "agAsset" ] },
                    1, 
                    0
                ] 
            }
        }
    }}, 
    { "$unwind": "$sessionId" }, 
    { "$group": { 
        "_id": "$_id", 
        "viewCount": { "$first": "$viewCount" },  
        "sessionCount": { "$sum": 1 } 
    }}
])

返回:

{ "_id" : "55f996a4e4b0cc9c0a392593", "viewCount" : 1, "sessionCount" : 2 }