Javascript 如何在两个聚合之间进行操作

Javascript 如何在两个聚合之间进行操作,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我有这个MongoDB模式 { "_id": { "$oid": "56cd9c103ff0020300fa1135" }, "title": "50x kliky", "timing": "2x týdně", "user": "Stejk", "history": [ { "title": "50x kliky", "datum": "11.09.2016", "done": false }, {

我有这个MongoDB模式

    {
"_id": {
    "$oid": "56cd9c103ff0020300fa1135"
},
"title": "50x kliky",
"timing": "2x týdně",
"user": "Stejk",
"history": [
    {
        "title": "50x kliky",
        "datum": "11.09.2016",
        "done": false
    },
    {
        "title": "50x kliky",
        "datum": "11.09.2016",
        "done": true
    },
    {
        "title": "50x kliky",
        "datum": "11.09.2016",
        "done": true
    },
    {
        "title": "50x kliky",
        "datum": "11.09.2016",
        "done": true
    },
    {
        "title": "50x kliky",
        "datum": "11.09.2016",
        "done": true
    }
],
"__v": 0
}
我有这些密码

    BuzzerListekSchema.statics.getBuzzerListekDetailPercentage = function(req, response, res){
  // Function1
  this.aggregate([
    {$unwind: '$history'},
    {$match: {'history.done': true}}, //Filtr
    {$group: {_id: req.params.id, count: {$sum: 1}}}
 ], function(err, c){
   if(err){
     res.status(500).json({succes: false, msg: 'Error'});
   }
   else {
    // Here is result c
    // {_id: ubuiabiufbuiab, count: 5}
   }
 });

 // Function2
 this.aggregate([
   {$unwind: '$history'},
   {$group: {_id: req.params.id, count: {$sum: 1}}}
], function(err, c){
  if(err){
    res.status(500).json({succes: false, msg: 'Error'});
  }
  else {
    // Here is result c
    // {_id: ubuiabiufbuiab, count: 5}
  }
});
  //Here i want reach a percent of percent=Function1/(Function2/100) and send it by JSON
  //res.json({succes: false, msg: percent});

};
我想用这些Function1/(Function2/100)做一个JSON响应。 有关更多说明,请参阅代码内的注释。 如何从这两个函数中得到c? 请帮帮我,我完全不懂这些,所以要耐心。
非常感谢。

考虑运行以下管道以获得所需的结果:

BuzzerListekSchema.statics.getBuzzerListekDetailPercentage = function(req, response, res){
    var id = req.params.id;
    var pipeline = [
        { "$match": { "_id": id } },
        { "$unwind": "$history" },          
        {
            "$group": {
                "_id": 0,
                "count": { 
                    "$sum": { 
                        "$cond": [
                            { "$eq": [ "$history.done", true ] },
                            1,
                            0
                        ]
                    } 
                },
                "total": { "$sum": 1 }
            }
        },
        {
            "$project": {
                "_id": 0,
                "percentage":{
                    "$multiply": [
                        { "$divide": [ 100, "$total" ] }, "$count"
                    ]
                }
            }
        }
    ]

    this.aggregate(pipeline)
        .exec(function (err, result){
            if(err){
                res.status(500).json({succes: false, msg: 'Error'});
            }
            else {
                res.json({succes: false, msg: result[0]})
            }
        });
}
谢谢@chridam

BuzzerListekSchema.statics.getBuzzerListekDetailPercentage = function(req, response, res){
    var id = req.params.id;
    var pipeline = [
        {"$match": {"_id": mongoose.Types.ObjectId(id)}},
        { "$unwind": "$history" },
        {
            "$group": {
                "_id": null,
                "count": {
                    "$sum": {
                        "$cond": [
                            { "$eq": [ "$history.done", true ] },
                            1,
                            0
                        ]
                    }
                },
                "total": { "$sum": 1 }
            }
        },
        {
            "$project": {
                "_id": 0,
                "percentage":{
                    "$multiply": [
                        { "$divide": [ 100, "$total" ] }, "$count"
                    ]
                }
            }
        }
    ];

    this.aggregate(pipeline)
        .exec(function (err, result){
            if(err){
                res.status(500).json({succes: false, msg: err});
            }
            else {
                res.status(200).json({succes: true, msg: result[0]})
            }
        });
};

如果我在req.params.id中有id,那么就替换其中的0,或者更好地解释一下我是否想要精确的百分比。非常感谢它的工作,但所有的东西在计划中请帮助我:(我迷路了:)再次。,,。。。我尝试用_id:req.params.id替换_id:0,但什么也没有changed@JakubStejskal很难准确地理解您想要的内容,所以我只是做了一个粗略的猜测,并包含了
$match
过滤器,用于计算特定文档的百分比。参见编辑的答案。对于MongoDB 3.2,这只是一个管道阶段:
{“$project”:{“百分比”:{“$multiply”:[{“$divide”:[{“$size”:{“$filter”:{“input”:“history”,“as”:“el”,“cond”:“$$el.done”}},{“$size”:“$history”},100]}
。这大大优于使用
$unwind
进行的任何处理。是时候升级了。希望一次可以在多个文档上执行此操作,否则在客户端代码中会更好。