Javascript 嵌套数组的计数
我有一个MongoDB模式蜂鸣器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.20
{
"_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
}
我想得到done为真的数组数。怎么做,我完全迷路了。我现在不知道如何使它发生。请帮忙
非常感谢您的帮助请尝试通过以下方式完成
Buzzer.aggregate([
// unwind the history array
{$unwind: '$history'},
// filter the done field is true document
{$match: {'history.done': true}},
// count the result
{$group: {_id: '$_id', count: {$sum: 1}}}
], function(err, doc) {
});
在纯Javascript中,您可以使用:
var数据={u id:{oid:“56CD9C103FF002300FA1135”},“title:“50x kliky”,“time:“2x týdne”,“user:”Stejk”,“history:“[{”title:“50x kliky”,“datum:“11.09.2016”,“done:”false:“{”title:“50x kliky”,“datum:“11.09.2016”,“done:”真“,”title:“2016”,“done:”“50x kliky”,“数据”:“2016年9月11日”,“完成”:真实},{“标题”:“50x kliky”,“数据”:“2016年9月11日”,“完成”:真实}],“u v”:0},
count=data.history.reduce(函数(r,a){
返回r+a.done;
}, 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});
};