如何计算 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”和“赛季”中包含的进球的平均值。结果应该是 33/5 = 6.6.
注意:对其进行平均的文档大小不同,即“Season”数组可以包含不同数量的文档并且不固定。
在这种情况下,我该如何计算这个平均值?如何使用 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"
]
}
}
}
])
这里是 post:
请检查这是否适合您:
[
{
$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"
}
]
}
}
}
]
我的数据库中有此文档:
[
{
"_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”和“赛季”中包含的进球的平均值。结果应该是 33/5 = 6.6.
注意:对其进行平均的文档大小不同,即“Season”数组可以包含不同数量的文档并且不固定。
在这种情况下,我该如何计算这个平均值?如何使用 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"
]
}
}
}
])
这里是 post:
请检查这是否适合您:
[
{
$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"
}
]
}
}
}
]