如何在mongoDB中对文档进行分组并获取文档计数以及获取子文档的平均值

时间:2019-12-23 18:54:49

标签: mongodb mongoose

如何在MongoDB中对文档进行分组并获取数量,以及获取子文档的平均数量。

收藏

[{
    "type": "FeatureCollection",
    "cityName": "Bengaluru",
    "features": [
        {
            "type": "Feature",
            "id": 2085,
            "properties": {
                "countryName": "India",
                "continentName": "Asia"
            }
        },
        {
            "type": "Feature",
            "id": 2085,
            "properties": {
                "countryName": "India",
                "continentName": "Asia"
            }
        }
    ]
}
{
    "type": "FeatureCollection",
    "cityName": "Bengaluru",
    "features": [
        {
            "type": "Feature",
            "id": 2095,
            "properties": {
                "countryName": "India",
                "continentName": "Asia"
            }
        }
    ]
}

{
    "type": "FeatureCollection",
    "cityName": "Bengaluru",
    "features": [
        {
            "type": "Feature",
            "id": 2035,
            "properties": {
                "countryName": "India",
                "continentName": "Asia"
            }
        }
    ]
},
{
    "type": "FeatureCollection",
    "cityName": "Delhi",
    "features": [
        {
            "type": "Feature",
            "id": 2031,
            "properties": {
                "countryName": "India",
                "continentName": "Asia"
            }
        }
                {
            "type": "Feature",
            "id": 2032,
            "properties": {
                "countryName": "India",
                "continentName": "Asia"
            }
        }
    ]
}
...
]

预期结果

[
    {
        "cityName": "Bengaluru",
        "count": 3
        "avgFeatures": 1
    },
    {
        "cityName": "Delhi",
        "count":1
        "avgFeatures": 2
    },
]
在上面的示例中,

count: 1根据cityName对数据进行分组。例如:db.mycollection.aggregate({$project: { count: { $size:"$features" }}})

并且avg仅具有按cityName分组的所有文档的features数组计数的平均值。

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

您可以选中此Mongo PlayGround

db.collection.aggregate({
      $group: {
         _id: "$cityName",
          count: {
                $sum: 1
          },
          sumFeatures: {
                $sum: {
                     $size: "$features"
                }
          }
      }
 },
 {
      $project: {
          _id: 1,
           count: 1,
           avg: {
               $floor: {
                      $divide: [
                                 "$sumFeatures",
                                 "$count"
                               ]
               }
           }
      }
 })

答案 1 :(得分:1)

这给出了您需要的确切结果:

db.mycollection.aggregate([
    {
        "$group": {
            "_id": "$cityName",
            "count": {
                "$sum": 1
            },
            "featureCount": {
                "$push": {
                    "$size": "$features"
                }
            }
        }
    },
    {
        "$project": {
            "cityName": "$_id",
            "count": "$count",
            "avgFeatures": {
                "$round": [{
                    "$divide": [{
                        "$sum": "$featureCount"
                    }, "$count"]
                },0]
            },
            "_id": 0
        }
    }
])