【问题标题】:How to group documents and get the count of documents and also get Avg of sub-documents in mongoDB如何对文档进行分组并获取文档数量以及在 mongoDB 中获取子文档的平均值
【发布时间】:2019-12-23 18:54:49
【问题描述】:

如何在 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 数组计数。

我们将不胜感激任何形式的帮助。

【问题讨论】:

  • 为什么Bengaluru 的预期avgFeatures1 而不是1.33
  • 请解释什么必须代表计数(和 avgFeature)
  • Count 表示记录组,例如:存在 3 个 cityName:Bengaluru 对象。 & Avg 实际上是 1.33 对于班加罗尔我们可以做圆形吗?通过计算与分组对象相关的特征数组计数获得的平均值

标签: mongodb mongoose


【解决方案1】:

你可以查看这个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"
                               ]
               }
           }
      }
 })

【讨论】:

    【解决方案2】:

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

    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
            }
        }
    ])
    

    【讨论】:

    • 谢谢@Ryan 通过执行您的解决方案,我得到了[{ "_id" : null, "cityName" : null, "count" : 2, "avgFeatures" : 5 }] 结果但我的预期结果不同!你能检查一次吗?我忽略了$round:[...],因为我的 mongoDB 版本不支持它。
    • 查询有效。看这里:mongoplayground.net/p/S5CDS33oWT_ 我删除了那个回合。
    猜你喜欢
    • 1970-01-01
    • 2014-02-02
    • 1970-01-01
    • 2017-06-29
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多