MongoDB:计算作为文档列表嵌入的所有特定文档字段的平均值

时间:2013-12-18 08:51:24

标签: mongodb aggregation-framework

以下是mongodb中文档的结构:

{
    "_id" : NumberLong(1),
    "averageRating" : 2.5,
    "date" : ISODate("2013-11-12T02:15:14.448Z"),
    "filledBy" : "Degroote, Fred",
    "results" : [
        {
            "answer" : "Very caring doctor. I am happy",
            "name" : "comment",
            "type" : "string"
        },
        {
            "answer" : "4",
            "name" : "premises",
            "type" : "integer"
        },
        {
            "answer" : "5",
            "name" : "staff",
            "type" : "integer"
        },
        {
            "answer" : "0",
            "name" : "provider",
            "type" : "integer"
        },
        {
            "answer" : "1",
            "name" : "care",
            "type" : "integer"
        }
    ],
    "tenantId" : NumberLong(2),
    "token" : "38ae01f0-1203-4d10-9ff1-7c9fad1790cc",
    "version" : NumberLong(0)
}

我们需要计算具有tenantId值2的所有文档的结果中结果名称“staff”的答案分数的平均值。

我们写的mongodb查询是:

> db.surveyResults.aggregate({$match: {'tenantId': 2}}, {$unwind: '$results'}, {$match: {'results.type':'integer'}},{$group: {_id: '$results.name', averageSatisfaction: {$avg: '$results.answer'}}})

但是,上面的mongodb查询中的问题是字段答案是字符串类型。有没有办法解决这个问题而不改变字段答案的类型?

1 个答案:

答案 0 :(得分:1)

我认为这是一个丑陋的查询,但它确实有效:

db.surveyResults.aggregate(
    {$match: {'tenantId': 2}}, 
    {$unwind: '$results'}, 
    {$match: {'results.type':'integer'}},
    {$project:{
        "results.answer":1,
        "results.name":1,
        one:{$cond:[{$eq:["$results.answer","1"]},1,0]},
        two:{$cond:[{$eq:["$results.answer","2"]},2,0]},
        three:{$cond:[{$eq:["$results.answer","3"]},3,0]},
        four:{$cond:[{$eq:["$results.answer","4"]},4,0]},
        five:{$cond:[{$eq:["$results.answer","5"]},5,0]},
        six:{$cond:[{$eq:["$results.answer","6"]},6,0]},
        seven:{$cond:[{$eq:["$results.answer","7"]},7,0]},
        eight:{$cond:[{$eq:["$results.answer","8"]},8,0]},
        nine:{$cond:[{$eq:["$results.answer","9"]},9,0]},
        }  
    },
    {$project: {
        "results.name":1,         
        sum:{$add:["$one","$two","$three","$four","$five","$six","$seven","$eight","$nine"]}        
     }},
     {$group: 
         {_id: '$results.name', averageSatisfaction: {$avg: '$sum'}}


        }

       )

无论如何,最好的选择是将整数用作整数。