You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用MongoDB聚合查询计算各国足球运动员位置占比

计算足球运动员位置在所属国家的占比

需求说明

计算足球运动员的位置在所属国家中的占比,例如尼泊尔共有800名球员,其中仅20名是守门员,占比为2.5%。

输入数据集

[
  {
    "position":"defenser",
    "country":"Nepal"
  },
  {
    "position":"defenser",
    "country":"Nepal"
  },
  {
    "position":"goal keeper",
    "country":"Nepal"
  },
  {
    "position":"goal keeper",
    "country":"France"
  },
  {
    "position":"defenser",
    "country":"France"
  },
  {
    "position":"goal keeper",
    "country":"France"
  },
  {
    "position":"defenser",
    "country":"France"
  },
  {
    "position":"defenser",
    "country":"Nepal"
  }
]

数据统计逻辑

  • 尼泊尔总计4名球员,其中3名为防守球员(defenser),占比75%
  • 法国总计4名球员,其中2名为防守球员(defenser),占比50%

期望输出

{"country":"Nepal", "position": "defenser", "percentage":"75"}
{"country":"France", "position": "defenser", "percentage":"50"}

内容的提问来源于stack exchange,提问作者Gaurav Sisodia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 11:57:08