jjzjj

MongoDB 对同一文档中多个字段的结果进行分组和计算

coder 2023-10-31 原文

关于 MongoDB 聚合框架,我能得到你的帮助吗?我试图从以下游戏集合中构建一个英超联赛表:

{ 
    "_id" : ObjectId("5b39fec4b5f8df161d259f36"), 
    "gameWeek" : 1, 
    "homeTeam" : "Arsenal", 
    "awayTeam" : "Leicester", 
    "homeGoals" : 2, 
    "awayGoals" : 1 
}, { 
    "_id" : ObjectId("5b39ffc2b5f8df161d259f6d"), 
    "gameWeek" : 2, 
    "homeTeam" : "Arsenal", 
    "awayTeam" : "Sunderland", 
    "homeGoals" : 1, 
    "awayGoals" : 1 
}, { 
    "_id" : ObjectId("5b39ffe8b5f8df161d259f7f"), 
    "gameWeek" : 2, 
    "homeTeam" : "Sunderland", 
    "awayTeam" : "Manchester United", 
    "homeGoals" : 1, 
    "awayGoals" : 1 
}, { 
    "_id" : ObjectId("5b492cbea5aef964f0911cce"), 
    "gameWeek" : 1, 
    "homeTeam" : "Manchester United", 
    "awayTeam" : "Leicester", 
    "homeGoals" : 0, 
    "awayGoals" : 1 
}

我希望得到如下结果:

{ 
    "_id" : "Arsenal", 
    "team" : "Arsenal", 
    "gamesPlayed" : 2, 
    "goalsFor" : 3, 
    "goalsAgainst" : 2, 
    "goalsDifference" : 1, 
    "gamesWon" : 1, 
    "gamesDraw" : 1, 
    "gamesLost" : 0, 
    "points" : 4 
}, { 
    "_id" : "Leicester", 
    "team" : "Leicester", 
    "gamesPlayed" : 2, 
    "goalsFor" : 2, 
    "goalsAgainst" : 2, 
    "goalsDifference" : 0, 
    "gamesWon" : 1, 
    "gamesDraw" : 0, 
    "gamesLost" : 1, 
    "points" : 3 
}, { 
    "_id" : "Sunderland", 
    "team" : "Sunderland", 
    "gamesPlayed" : 2, 
    "goalsFor" : 2, 
    "goalsAgainst" : 2, 
    "goalsDifference" : 0, 
    "gamesWon" : 0, 
    "gamesDraw" : 2, 
    "gamesLost" : 0, 
    "points" : 2 
}, { 
    "_id" : "Manchester United", 
    "team" : "Manchester United", 
    "gamesPlayed" : 2, 
    "goalsFor" : 1, 
    "goalsAgainst" : 2, 
    "goalsDifference" : -1, 
    "gamesWon" : 0, 
    "gamesDraw" : 1, 
    "gamesLost" : 1, 
    "points" : 1 
}

哪里:

  • gamesPlayed - 玩过的游戏总数,
  • goalsFor - 团队的总进球数,
  • goalsAgainst - 总进球数,
  • goalsDifference - 'goalsFor' 减去 'goalsAgainst'
  • 积分 - 每赢一场比赛得 3 分,平局得 1 分。

到目前为止,我有以下关于构建团队支持 homeTeam 结果的查询:

db.football_matches.aggregate([
    { 
        $group: { 
            _id:  "$homeTeam",  
            gamesPlayed : { $sum: NumberInt(1) }, 
            goalsFor: { $sum:  "$homeGoals"  }, 
            goalsAgainst: { $sum:  "$awayGoals" }, 
            gamesWon: { $sum: { $cond: { if: { $gt: [ "$homeGoals",  "$awayGoals" ]}, then: NumberInt(1), else: NumberInt(0) } }}, 
            gamesDraw: { $sum: { $cond: { if: { $eq: [ "$homeGoals",  "$awayGoals" ]}, then: NumberInt(1), else: NumberInt(0) } }},
            gamesLost: { $sum: { $cond: { if: { $lt: [ "$homeGoals",  "$awayGoals" ]}, then: NumberInt(1), else: NumberInt(0) } }}
        }
     }, { 
         $project: { 
             team:  "$_id" , 
             gamesPlayed:  "$gamesPlayed", 
             goalsFor:  "$goalsFor", 
             goalsAgainst:  "$goalsAgainst", 
             goalsDifference: { $subtract: [ "$goalsFor", "$goalsAgainst"] }, 
             gamesWon: "$gamesWon", 
             gamesDraw: "$gamesDraw",
             gamesLost: "$gamesLost",
             points: { $add: [ {$multiply: [ "$gamesWon", NumberInt(3)]}, {$multiply: [ "$gamesDraw", NumberInt(1)]} ]}
        }
     }, {
       $sort: { points: -1, goalsDifference: -1 }
    }
])

理论上,我需要将以下分组结果与另一个类似的组语句结合起来,其中将对 awayTeam 字段执行类似的操作:

{
    $group: { 
        _id:  "$awayTeam",  
        gamesPlayed : { $sum: NumberInt(1) }, 
        goalsFor: { $sum:  "$awayGoals"  }, 
        goalsAgainst: { $sum:  "$homeGoals" }, 
        gamesWon: { $sum: { $cond: { if: { $gt: [ "$awayGoals",  "$homeGoals" ]}, then: NumberInt(1), else: NumberInt(0) } }}, 
        gamesDraw: { $sum: { $cond: { if: { $eq: [ "$awayGoals",  "$homeGoals" ]}, then: NumberInt(1), else: NumberInt(0) } }},
        gamesLost: { $sum: { $cond: { if: { $lt: [ "$awayGoals",  "$homeGoals" ]}, then: NumberInt(1), else: NumberInt(0) } }}
    }
 } 

我该怎么做?非常感谢你。如果之前有人问过类似的问题,请接受我的道歉。

最佳答案

您可以使用 $facet 尝试以下聚合, $replaceRoot , $unwind , $concatArrays最后还有一个$group阶段

db.collection.aggregate([
  { "$facet": {
    "first": [
      { "$group": {
        "_id": "$homeTeam",
        "gamesPlayed": { "$sum": 1 },
        "goalsFor": { "$sum": "$homeGoals" },
        "goalsAgainst": { "$sum": "$awayGoals" },
        "gamesWon": {
          "$sum": { "$cond": { "if": { "$gt": [ "$homeGoals", "$awayGoals" ] }, "then": 1, "else": 0 } }
        },
        "gamesDraw": {
          "$sum": { "$cond": { "if": { "$eq": [ "$homeGoals", "$awayGoals" ] }, "then": 1, "else": 0 } }
        },
        "gamesLost": {
          "$sum": { "$cond": { "if": { "$lt": [ "$homeGoals", "$awayGoals" ] }, "then": 1, "else": 0 } }
        }
      }},
      { "$project": {
        "team": "$_id",
        "gamesPlayed": "$gamesPlayed",
        "goalsFor": "$goalsFor",
        "goalsAgainst": "$goalsAgainst",
        "goalsDifference": { "$subtract": [ "$goalsFor", "$goalsAgainst" ] },
        "gamesWon": "$gamesWon",
        "gamesDraw": "$gamesDraw",
        "gamesLost": "$gamesLost",
        "points": { "$add": [{ "$multiply": [ "$gamesWon", 3 ] }, { "$multiply": [ "$gamesDraw", 1 ] }] }
      }},
      { "$sort": { "points": -1, "goalsDifference": -1 } }
    ],
    "second": [
      { "$group": {
        "_id": "$awayTeam",
        "gamesPlayed": { "$sum": 1 },
        "goalsFor": { "$sum": "$awayGoals" },
        "goalsAgainst": { "$sum": "$homeGoals" },
        "gamesWon": {
          "$sum": { "$cond": { "if": { "$gt": [ "$awayGoals", "$homeGoals" ] }, "then": 1, "else": 0 } }
        },
        "gamesDraw": {
          "$sum": { "$cond": { "if": { "$eq": [ "$awayGoals", "$homeGoals" ] }, "then": 1, "else": 0 } }
        },
        "gamesLost": {
          "$sum": { "$cond": { "if": { "$lt": [ "$awayGoals", "$homeGoals" ] }, "then": 1, "else": 0 } }
        }
      }},
      { "$project": {
        "team": "$_id",
        "gamesPlayed": "$gamesPlayed",
        "goalsFor": "$goalsFor",
        "goalsAgainst": "$goalsAgainst",
        "goalsDifference": { "$subtract": [ "$goalsFor", "$goalsAgainst" ] },
        "gamesWon": "$gamesWon",
        "gamesDraw": "$gamesDraw",
        "gamesLost": "$gamesLost",
        "points": { "$add": [{ "$multiply": [ "$gamesWon",3 ] }, { "$multiply": [ "$gamesDraw",1 ] } ] }
      }},
      { "$sort": { "points": -1, "goalsDifference": -1 } }
    ]
  }},
  { "$project": {
    "data": {
      "$concatArrays": [ "$first", "$second" ]
    }
  }},
  { "$unwind": "$data" },
  { "$replaceRoot": { "newRoot": "$data" } },
  { "$group": {
    "_id": "$_id",
    "gamesPlayed": { "$sum": "$gamesPlayed" },
    "goalsFor": { "$sum": "$goalsFor" },
    "goalsAgainst": { "$sum": "$goalsAgainst" },
    "gamesWon": { "$sum": "$gamesWon" },
    "gamesDraw": { "$sum": "$gamesDraw" },
    "gamesLost": { "$sum": "$gamesLost" }
  }}
])

关于MongoDB 对同一文档中多个字段的结果进行分组和计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52060011/

有关MongoDB 对同一文档中多个字段的结果进行分组和计算的更多相关文章

  1. ruby-on-rails - 使用 Ruby on Rails 进行自动化测试 - 最佳实践 - 2

    很好奇,就使用ruby​​onrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提

  2. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  3. ruby-on-rails - 按天对 Mongoid 对象进行分组 - 2

    在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev

  4. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  5. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  6. ruby - 使用 C 扩展开发 ruby​​gem 时,如何使用 Rspec 在本地进行测试? - 2

    我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当

  7. ruby - Highline 询问方法不会使用同一行 - 2

    设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案

  8. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  9. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  10. ruby-on-rails - 使用一系列等级计算字母等级 - 2

    这里是Ruby新手。完成一些练习后碰壁了。练习:计算一系列成绩的字母等级创建一个方法get_grade来接受测试分数数组。数组中的每个分数应介于0和100之间,其中100是最大分数。计算平均分并将字母等级作为字符串返回,即“A”、“B”、“C”、“D”、“E”或“F”。我一直返回错误:avg.rb:1:syntaxerror,unexpectedtLBRACK,expecting')'defget_grade([100,90,80])^avg.rb:1:syntaxerror,unexpected')',expecting$end这是我目前所拥有的。我想坚持使用下面的方法或.join,

随机推荐