如何聚合总计百分比

时间:2017-05-22 04:10:34

标签: node.js mongodb mongodb-query aggregation-framework

这是我的JSON数据:

    [
      {
        "_id": "2017/4/7",
        "category": "science",
        "presenties ": 20,
        "absentee": 1
      },
      {
        "_id": "2017/4/19",
        "category": "science",
        "presenties ": 11,
        "absentee": 32
      },
      {
        "_id": "2017/4/19",
        "category": "Commerce",
        "presenties ": 10,
        "absentee": 32
      },

      {
        "_id": "2017/4/7",
        "category": "Arts",
        "presenties ": 20,
        "absentee": 16
      },
      {
        "_id": "2017/4/19",
        "category": "Arts",
        "presenties ": 10,
        "absentee": 21
      }
    ]

我想显示:    1)每天的每个类别的参赛者和缺席者的百分比,我尝试了综合功能,所以我得到总的参赛者和缺席者

[{
        "_id": "2017/4/7",
        "total_presenties ": 40,
        "total_absentee": 17
      },
      {
        "_id": "2017/4/19",
        "total_presenties ": 31,
        "total_absentee": 85
      }
    ]

2)但需要根据这样的日期提及上述JSON中的百分比

[{
            "_id": "2017/4/7",
                "category": "science",
                "presenties ": 10,
                "absentee": 21
               "presenties_Precentage":xx%
               "absentees_Precentage": xx%
          },{"_id": "2017/4/7",
                "category": "commerce",
                "presenties ": 10,
                "absentee": 21
               "presenties_Precentage":xx%
               "absentees_Precentage": xx%},{
                "_id": "2017/4/19",
                "category": "Arts",
                "presenties ": 10,
                "absentee": 21
               "presenties_Precentage":xx%
               "absentees_Precentage": xx%
              }]

请在此帮助我完成此任务

2 个答案:

答案 0 :(得分:1)

只是尝试从您的问题中尽可能多地解释,似乎您想要汇总每个类别每天的总计,但也会显示"所有"的总日期百分比。类别。

如果类别数据在一天内没有超过BSON限制,那么此过程将是每个类别首先聚合两个$group阶段,然后每天汇总。然后可以使用总日数来获得百分比。

collection.aggregate([
  { "$group": {
    "_id": {
       "date": "$_id",
       "category": "$category"
     },
    "presenties ": { "$sum": "$presenties" },
    "absentee": { "$sum": "$absentee" }
  }},
  { "$group": {
    "_id": "$_id.date",
    "categories": {
      "$push": {
        "category": "$_id.category"m
        "presenties ": "$presenties",
        "absentee": "$absentee"
      }
    },
    "total_presenties": { "$sum": "$presenties" },
    "total_absentee": { "$sum": "$absentee" }
  }},
  { "$unwind": "$categories" },
  { "$project": {
    "category": "$categories.category",
    "presenties": "$categories.presenties",
    "presenties_Percentage": {
      "$multiply": [
        { "$divide": [ "$categories.presenties", "$total_presenties" ] },
        100
      ]
    },
    "absentee": "$categories.absentee",
    "absentee_Percentage": {
      "$multiply": [
        { "$divide": [ "$categories.absentee", "$total_absentee" ] },
        100
      ]
    }
  }

],function(err,results) {

})

所以这里的关键是将所有内容组合在一起以获得"总数"然后你就可以从存在的数字中进行数学运算。

答案 1 :(得分:-1)

根据类别&获得结果百分比日期使用低于逻辑

    db.collectionName.aggregate({$match:{'category': 'commerce','_id': 
    '2017/4/19' } },{"$project":{"count":1,"percentage":{"$multiply":
    [{"$divide":[100,totalStudent]},"$count"]}}})

您需要传递类别以及您需要数据的数据。要获得百分比,您还需要定义学生总数。

这个过程很复杂。

您还可以根据类别和日期查找集合,并计算节点服务器上的百分比而不是mongodb。