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

huangapple go评论143阅读模式
英文:

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

问题

我在我的数据库中有这个文档:

  1. [
  2. {
  3. "_id": {
  4. "$oid": "5f5f280ffa2236115655cb6a"
  5. },
  6. "Name": "Rovilio Chipman",
  7. "Last_season": {
  8. "year": "2010-2011",
  9. "goals": 10,
  10. "assists": 1
  11. },
  12. "Last_season_2": {
  13. "year": "2011-2012",
  14. "goals": 1,
  15. "assists": 12
  16. },
  17. "Seasons": [
  18. {
  19. "year": "2012-2013",
  20. "goals": 11,
  21. "assists": 4
  22. },
  23. {
  24. "year": "2013-2014",
  25. "goals": 6,
  26. "assists": 2
  27. },
  28. {
  29. "year": "2014-2015",
  30. "goals": 5,
  31. "assists": 5
  32. }
  33. ]
  34. }
  35. ]

我想获取所有目标的平均值,即在 "Last_season"、"Last_season_2" 和 "Seasons" 中包含的目标的平均值。结果应为 33/5 = 6.6。

注意:用于计算此平均值的文档大小不同,即 "Seasons" 数组可以包含不同数量的文档且不固定。

在这种情况下,我如何计算这个平均值?我如何使用 Java Driver 编写代码?

英文:

I have this document in my database:

  1. [
  2. {
  3. "_id": {
  4. "$oid": "5f5f280ffa2236115655cb6a"
  5. },
  6. "Name": "Rovilio Chipman",
  7. "Last_season": {
  8. "year": "2010-2011",
  9. "goals": 10,
  10. "assists": 1
  11. },
  12. "Last_season_2": {
  13. "year": "2011-2012",
  14. "goals": 1,
  15. "assists": 12
  16. },
  17. "Seasons": [
  18. {
  19. "year": "2012-2013",
  20. "goals": 11,
  21. "assists": 4
  22. },
  23. {
  24. "year": "2013-2014",
  25. "goals": 6,
  26. "assists": 2
  27. },
  28. {
  29. "year": "2014-2015",
  30. "goals": 5,
  31. "assists": 5
  32. }
  33. ]
  34. }
  35. ]

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". The result should be 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?

答案1

得分: 3

  1. 首先您需要将所有值放入一个数组中然后可以计算平均值这可能是一个解决方案
  2. db.collection.aggregate([
  3. {
  4. $set: {
  5. AllSeasons: {
  6. $concatArrays: [
  7. "$Seasons",
  8. [ "$Last_season" ],
  9. [ "$Last_season_2" ]
  10. ]
  11. }
  12. }
  13. },
  14. { $set: { average: { $avg: [ "$AllSeasons.goals" ] } } },
  15. { $unset: "AllSeasons" }
  16. ])

Mongo Playground链接

  1. <details>
  2. <summary>英文:</summary>
  3. First you need to put all values in one array, then you can calculate the average. This could be one solution:
  4. db.collection.aggregate([
  5. {
  6. $set: {
  7. AllSeasons: {
  8. $concatArrays: [
  9. &quot;$Seasons&quot;,
  10. [ &quot;$Last_season&quot; ],
  11. [ &quot;$Last_season_2&quot; ]
  12. ]
  13. }
  14. }
  15. },
  16. { $set: { average: { $avg: [ &quot;$AllSeasons.goals&quot; ] } } },
  17. { $unset: &quot;AllSeasons&quot; }
  18. ])
  19. [Mongo Playground][1]
  20. [1]: https://mongoplayground.net/p/QLsvSsBoAGz
  21. </details>
  22. # 答案2
  23. **得分**: 1
  24. 你可以使用Mongo聚合来实现这个。
  25. ```json
  26. db.collection.aggregate([
  27. {
  28. "$project": {
  29. /* 在Seasons列表中求目标之和 */
  30. "seasons_goals": {
  31. "$sum": [
  32. "$Seasons.goals"
  33. ]
  34. },
  35. /* 计算季节数量:Seasons的长度 + 2 */
  36. "nb_seasons": {
  37. "$sum": [
  38. {
  39. "$size": "$Seasons"
  40. },
  41. 2
  42. ]
  43. },
  44. /* 求最后两个季节的目标之和 */
  45. "total": {
  46. "$sum": [
  47. "$Last_season.goals",
  48. "$Last_season_2.goals"
  49. ]
  50. }
  51. }
  52. },
  53. /* 通过将seasons_goals+total除以nb_seasons来计算平均值 */
  54. {
  55. "$project": {
  56. "result": {
  57. "$divide": [
  58. {
  59. "$sum": [
  60. "$seasons_goals",
  61. "$total"
  62. ]
  63. },
  64. "$nb_seasons"
  65. ]
  66. }
  67. }
  68. }
  69. ])

试一试


这里有一篇关于:使用Java驱动程序进行MongoDB聚合

英文:

You can do that with a mongo aggregate.

  1. db.collection.aggregate([
  2. {
  3. &quot;$project&quot;: {
  4. /* Summing goals in the Seasons list */
  5. &quot;seasons_goals&quot;: {
  6. &quot;$sum&quot;: [
  7. &quot;$Seasons.goals&quot;
  8. ]
  9. },
  10. /* Counting the number of seasons: length of Seasons + 2 */
  11. &quot;nb_seasons&quot;: {
  12. &quot;$sum&quot;: [
  13. {
  14. &quot;$size&quot;: &quot;$Seasons&quot;
  15. },
  16. 2
  17. ]
  18. },
  19. /* Summing goals of the two last seasons */
  20. &quot;total&quot;: {
  21. &quot;$sum&quot;: [
  22. &quot;$Last_season.goals&quot;,
  23. &quot;$Last_season_2.goals&quot;
  24. ]
  25. }
  26. }
  27. },
  28. /* Calculate the average by dividing seasons_goals+total by nb_seasons */
  29. {
  30. &quot;$project&quot;: {
  31. &quot;result&quot;: {
  32. &quot;$divide&quot;: [
  33. {
  34. &quot;$sum&quot;: [
  35. &quot;$seasons_goals&quot;,
  36. &quot;$total&quot;
  37. ]
  38. },
  39. &quot;$nb_seasons&quot;
  40. ]
  41. }
  42. }
  43. }
  44. ])

try it


And here is a post on: MongoDB aggregation with Java driver

答案3

得分: 1

请检查一下这是否适用于您:

  1. [
  2. {
  3. "$set": {
  4. "Seasons": {
  5. "$concatArrays": [
  6. "$Seasons",
  7. [
  8. "$Last_season_2",
  9. "$Last_season"
  10. ]
  11. ]
  12. }
  13. }
  14. },
  15. {
  16. "$project": {
  17. "Name": 1,
  18. "avgGoals": {
  19. "$divide": [
  20. {
  21. "$reduce": {
  22. "input": "$Seasons",
  23. "initialValue": 0,
  24. "in": {
  25. "$sum": [
  26. "$$this.goals",
  27. "$$value"
  28. ]
  29. }
  30. }
  31. },
  32. {
  33. "$size": "$Seasons"
  34. }
  35. ]
  36. }
  37. }
  38. }
  39. ]

Mongo Playground

英文:

Please check if this works for you:

  1. [
  2. {
  3. $set: {
  4. &quot;Seasons&quot;: {
  5. $concatArrays: [
  6. &quot;$Seasons&quot;,
  7. [
  8. &quot;$Last_season_2&quot;,
  9. &quot;$Last_season&quot;
  10. ]
  11. ]
  12. }
  13. }
  14. },
  15. {
  16. $project: {
  17. &quot;Name&quot;: 1,
  18. &quot;avgGoals&quot;: {
  19. $divide: [
  20. {
  21. $reduce: {
  22. input: &quot;$Seasons&quot;,
  23. initialValue: 0,
  24. in: {
  25. $sum: [
  26. &quot;$$this.goals&quot;,
  27. &quot;$$value&quot;
  28. ]
  29. }
  30. }
  31. },
  32. {
  33. $size: &quot;$Seasons&quot;
  34. }
  35. ]
  36. }
  37. }
  38. }
  39. ]

Mongo Playground

huangapple
  • 本文由 发表于 2020年9月15日 02:11:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63889697.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定