【问题标题】:How to Sum all of the values in a collection in MongoDB如何在 MongoDB 中对集合中的所有值求和
【发布时间】:2017-09-27 22:50:41
【问题描述】:

我在 mongo 中收集了数学、写作和阅读成绩优异的学生。我需要总结所有的数学、写作和阅读成绩。

例如,如果 3 名学生的数学各得 50,那么总和就是 150。

到目前为止我有:

db.student.aggregate([{$group:{"_id":null, total:{$sum:"$math score"}}}])

我在 Google 上花了大约一个小时试图弄清楚这一点。我使用 mongo 的经验为零,所以进展缓慢。

这是我的数据示例

{
 "student_data":
 [
  {
   "S-ID": 91371,
   "gender": "male",
   "race/ethnicity": "group B",
   "parental level of education": "some college",
   "lunch": "standard",
   "test preparation course": "completed",
   "math score": 44,
   "reading score": 50,
   "writing score": 48,
   "Total Score": 142
},

【问题讨论】:

  • 您能否提供一个示例数据库数据,以便我了解数据的存储方式
  • 我添加了一些示例数据,我想我搞砸了,因为数据在数组中。
  • 我已经添加了一个答案请检查它@hunter

标签: mongodb sum


【解决方案1】:

试穿一下尺寸:

db = db.getSiblingDB("testX");

function show(c,emit=true) {
  var nn = 0;
  c.forEach(function(r) {
        nn++;
        if(emit == true) {
            printjson(r);
        }
    });
print("found " + nn);
};

db.foo.drop(); // CAREFUL! This drops "foo" every time!

var students = [
            {_id:1, studentId:"A1", math:10, writing:15, reading: 17}
           ,{_id:2, studentId:"A2", math:12, writing:17, reading: 19}
           ,{_id:3, studentId:"A3", math:14, writing:19, reading: 21}
           ,{_id:4, studentId:"A4", math:16, writing:21, reading: 23}
            ];

db.foo.insert(students);

c = db.foo.aggregate([
  {$group: {_id: null, totMath: {$sum: "$math"}, totWrite: {$sum: "$writing"}, totRead: {$sum: "$reading"}}}
                  ]);

show(c, true);

【讨论】:

    【解决方案2】:

    如果你在 db 中有如下这样的数据

         {"student_data":[{
           "S-ID": 91371,
           "gender": "male",
           "race/ethnicity": "group B",
           "parental level of education": "some college",
           "lunch": "standard",
           "test preparation course": "completed",
           "math score": 44,
           "reading score": 50,
           "writing score": 48,
           "Total Score": 142
        },{
           "S-ID": 91371,
           "gender": "male",
           "race/ethnicity": "group B",
           "parental level of education": "some college",
           "lunch": "standard",
           "test preparation course": "completed",
           "math score": 44,
           "reading score": 50,
           "writing score": 48,
           "Total Score": 142
        },{
           "S-ID": 91371,
           "gender": "male",
           "race/ethnicity": "group B",
           "parental level of education": "some college",
           "lunch": "standard",
           "test preparation course": "completed",
           "math score": 44,
           "reading score": 50,
           "writing score": 48,
           "Total Score": 142
        }]
    
    
    
       db.collection.aggregate([
        {$unwind:'student_data'},
        {$group:
        {_id:'null',
        {count:{$sum:$math score}
        }}
        }])
    

    【讨论】:

    • 运行命令时出现错误 [thread1] SyntaxError: invalid property id @(shell):5:4。我还确保将集合更改为实际集合名称。
    • 我在 python 中尝试了相同的命令并收到 [{'_id': None, 'total': 0}]
    • 我想通了,我需要在组中添加 $student_data.math 分数。感谢您的帮助。
    猜你喜欢
    • 2011-06-05
    • 2020-11-29
    • 1970-01-01
    • 2015-01-28
    • 2022-12-08
    • 2012-06-30
    • 2019-07-03
    • 2014-08-15
    • 2018-03-04
    相关资源
    最近更新 更多