简体   繁体   English

如何计算文档数组外的字段与 MongoDB 和 Java 中的文档数组内的字段之间的平均值?

[英]How can I calculate the average between fields outside a document array with those inside a documents array in MongoDB and Java?

I have this document in my database:我的数据库中有这个文件:

[
  {
    "_id": {
      "$oid": "5f5f280ffa2236115655cb6a"
    },
    "Name": "Rovilio Chipman",
    "Last_season": {
      "year": "2010-2011",
      "goals": 10,
      "assists": 1
    },
    "Last_season_2": {
      "year": "2011-2012",
      "goals": 1,
      "assists": 12
    },
    "Seasons": [
      {
        "year": "2012-2013",
        "goals": 11,
        "assists": 4
      },
      {
        "year": "2013-2014",
        "goals": 6,
        "assists": 2
      },
      {
        "year": "2014-2015",
        "goals": 5,
        "assists": 5
      }
    ]
  }
]

I would like to get the average of all goals, ie the average of the goals included in "Last_season", "Last_season_2" and "Season".我想获得所有目标的平均值,即“Last_season”、“Last_season_2”和“Season”中包含的目标的平均值。 The result should be 33/5 = 6.6.结果应该是 33/5 = 6.6。

NB: the documents on which to make this average are of different size, ie the "Season" array can contain a different number of documents and not fixed.注意:要对其进行平均的文档大小不同,即“季节”数组可以包含不同数量的文档并且不固定。

How do I calculate this average in this case?在这种情况下我如何计算这个平均值? How do I code it with Java Driver?如何使用 Java 驱动程序对其进行编码?

First you need to put all values in one array, then you can calculate the average.首先,您需要将所有值放在一个数组中,然后您可以计算平均值。 This could be one solution:这可能是一种解决方案:

db.collection.aggregate([
  {
    $set: {
      AllSeasons: {
        $concatArrays: [
          "$Seasons",
          [ "$Last_season" ],
          [ "$Last_season_2" ]
        ]
      }
    }
  },
  { $set: { average: { $avg: [ "$AllSeasons.goals" ] } } },
  { $unset: "AllSeasons" }
])

Mongo Playground蒙戈游乐场

You can do that with a mongo aggregate.您可以使用 mongo 聚合来做到这一点。

db.collection.aggregate([
  {
    "$project": {
      /* Summing goals in the Seasons list */
      "seasons_goals": { 
        "$sum": [
          "$Seasons.goals"
        ]
      },

      /* Counting the number of seasons: length of Seasons + 2 */
      "nb_seasons": {
        "$sum": [
          {
            "$size": "$Seasons"
          },
          2
        ]
      },

      /* Summing goals of the two last seasons */
      "total": {
        "$sum": [
          "$Last_season.goals",
          "$Last_season_2.goals"
        ]
      }
    }
  },
  /* Calculate the average by dividing seasons_goals+total by nb_seasons */
  {
    "$project": {
      "result": {
        "$divide": [
          {
            "$sum": [
              "$seasons_goals",
              "$total"
            ]
          },
          "$nb_seasons"
        ]
      }
    }
  }
])

try it尝试一下


And here is a post on: MongoDB aggregation with Java driver这是一篇关于: MongoDB 聚合与 Java 驱动程序的帖子

Please check if this works for you:请检查这是否适合您:

[
  {
    $set: {
      "Seasons": {
        $concatArrays: [
          "$Seasons",
          [
            "$Last_season_2",
            "$Last_season"
          ]
        ]
      }
    }
  },
  {
    $project: {
      "Name": 1,
      "avgGoals": {
        $divide: [
          {
            $reduce: {
              input: "$Seasons",
              initialValue: 0,
              in: {
                $sum: [
                  "$$this.goals",
                  "$$value"
                ]
              }
            }
          },
          {
            $size: "$Seasons"
          }
        ]
      }
    }
  }
]

Mongo Playground蒙戈游乐场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM