簡體   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?

我的數據庫中有這個文件:

[
  {
    "_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
      }
    ]
  }
]

我想獲得所有目標的平均值,即“Last_season”、“Last_season_2”和“Season”中包含的目標的平均值。 結果應該是 33/5 = 6.6。

注意:要對其進行平均的文檔大小不同,即“季節”數組可以包含不同數量的文檔並且不固定。

在這種情況下我如何計算這個平均值? 如何使用 Java 驅動程序對其進行編碼?

首先,您需要將所有值放在一個數組中,然后您可以計算平均值。 這可能是一種解決方案:

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

蒙戈游樂場

您可以使用 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"
        ]
      }
    }
  }
])

嘗試一下


這是一篇關於: MongoDB 聚合與 Java 驅動程序的帖子

請檢查這是否適合您:

[
  {
    $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"
          }
        ]
      }
    }
  }
]

蒙戈游樂場

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM