MongoDB - 分割一组文档中另一个特征的最大值后的特征平均值

MongoDB - average of a feature after slicing the max of another feature in a group of documents

我是 mongodb 的新手,正在尝试解决几个问题,但我什至不确定它们是否可行。

每个文件的结构是:

{
     "_id"  : {
              "$oid": Text
              },
     "grade": Text,
     "type" : Text,
     "score": Integer,
     "info" : {
              "range"  : NumericText,
              "genre"  : Text,
              "special": {keys:values}
              }
};

第一个查询会给我:

我试过类似下面的方法,但显然是错误的:

collection.aggregate([{
                       '$group': {'_id':'$grade',
                                  'highest_range': {'$max':'$info',
                                  'average_score': {'$avg':'$score'}}}
                       }])

第二个查询将给出不同的 genre 条记录。

任何帮助都是有价值的!


ADDITION - 提供文档示例和输出:

{
     "_id"  : {
              "$oid": '60491ea71f8'
              },
     "grade": D,
     "type" : Shop,
     "score": 4,
     "info" : {
              "range"  : "2",
              "genre"  : 'Pet shop',
              "special": {'ClientsParking':True,
                          'AcceptsCreditCard':True,
                          'BikeParking':False}
                         }
};

我正在查看的输出是几行内的内容:

[{grade: A, "highest_range":"4", "average_score":3.5},
 {grade: B, "highest_range":"7", "average_score":8.3},
 {grade: C, "highest_range":"3", "average_score":2.4}]

我想你正在寻找这个:

db.collection.aggregate([
   {
      '$group': {
         '_id': '$grade',
         'highest_range': { '$max': '$info.range' },
         'average_score': { '$avg': '$score' }
      }
   }
])

但是,$min$max$avg 仅适用于数字,不适用于字符串。 您可以尝试 { '$first': '$info.range' }{ '$last': '$info.range' }。但它需要 $sort 才能获得正确的结果。不清楚你所说的“最高范围”是什么意思。