使用mongo和angular进行聚合的自定义数学运算

时间:2017-04-11 23:28:10

标签: angularjs mongodb mongoose

我目前正在使用聚合来显示统计页面的注册团队和网络。我可以在每个级别进行计数,但网络的计算是不准确的。我的汇总如下:

module.exports.registrationStats = function(req, res) {
  Registration.aggregate([
    {
        "$group": {
            "_id": { 
                "day": "$day", 
                "group": "$group",
                "division": "$division",
                "level": "$level"
            },
            "count": { "$sum": 1 }
        }
    },
    {
        "$group": {
            "_id": { 
                "day": "$_id.day", 
                "group": "$_id.group",
                "division": "$_id.division"                
            },
            "count": { "$sum": "$count" },
            "levels": {
                "$push": {
                    "level": "$_id.level",
                    "teams": "$count",
                    "nets" : {$ceil : { $divide: [ "$count" , 5 ] } }
                }
            }
        }
    },
    {
        "$group": {
            "_id": { 
                "day": "$_id.day", 
                "group": "$_id.group"             
            },
            "count": { "$sum": "$count" },
            "divisions": {
                "$push": {
                    "division": "$_id.division",
                    "count": "$count",
                    "levels": "$levels"
                }
            }
        }
    }
]).exec(function(err, regStats){
    if(err) {
      console.log("Error grouping registrations");
      res.status(500).json(err);
    } else {
      console.log("Found and grouped " + regStats.length + " regStats");
      res.json(regStats);
    }
  });
};

这给了我以下输出:

[
    {
        "_id": {
            "day": "Saturday",
            "group": "nonpro"
        },
        "count": 144,
        "divisions": [
            {
                "division": "Men's",
                "count": 69,
                "levels": [
                    {
                        "level": "BB",
                        "teams": 30,
                        "nets": 6
                    },
                    {
                        "level": "A",
                        "teams": 8,
                        "nets": 2
                    },
                    {
                        "level": "B",
                        "teams": 19,
                        "nets": 4
                    },
                    {
                        "level": "AA",
                        "teams": 12,
                        "nets": 3
                    }
                ]
            },
            {
                "division": "Women's",
                "count": 75,
                "levels": [
                    {
                        "level": "AA",
                        "teams": 9,
                        "nets": 2
                    },
                    {
                        "level": "BB",
                        "teams": 16,
                        "nets": 4
                    },
                    {
                        "level": "B",
                        "teams": 18,
                        "nets": 4
                    },
                    {
                        "level": "A",
                        "teams": 32,
                        "nets": 7
                    }
                ]
            }
        ]
    }
]

问题是我不能在Math.ceil(divisions.count / 5)上运行ceil过滤器来获取divisions.nets或Math.ceil(_id.count / 5)的值来获取_id.nets因为在某些情况下他们错了。

我需要能够将divisions.levels.nets总计并将其推送到divisions.nets并添加divisions.nets并将该值放入_id.nets以便计算正常工作。

关于如何做到这一点的任何想法?

1 个答案:

答案 0 :(得分:2)

像这样的东西。添加$project阶段以计算nets的{​​{1}},然后level&其余$sum

$push nets
$groups