计算内部数组猫鼬

时间:2018-10-19 18:55:23

标签: arrays mongodb count aggregation-framework

我想对数组进行计数。这是我的模特

modelDetail.aggregate([
            {
                $group: {
                    _id: '$main_section.dept',  
                    count: {$sum: 'first_array.second_array.main_data'}
                }
            }
        ], function (err, result) {
                if (err) {
                    //next(err);
                    console.log(err);
                } else {
                    console.log(JSON.stringify(result));
                    res.json(result);
                }
        });

这将为所有结果带来0。输出低于

[{"_id":"design","count":0},
{"_id":"training","count":0},
{"_id":"forecast","count":0},
{"_id":"internal audit","count":0},
{"_id":"research","count":0}]

我想获取所有数据的计数

这是我的模式

var userSchema = mongoose.Schema({
    main_section        :{
        dept            : String
    },

    first_array :[{//
        floor           : String,
        second_array            :[{//3rd level
            name                : String,//4th level
            date            : String
        }]
    }]
});

this is the data in the json

{ "_id" : "5bb39baf40f87f17f01734f8",
    "main_section" : {
        "dept" : "design"
    }, 
    "first_array" : [{ "_id" : "5bb39baf40f87f17f01734fc",  "floor" : "2nd floor" ,
        "second_array" : [
            { "_id" : "5bc102f6dff41a0e844dd2b7", "name" : "Blake Tyson", "date" : "2018-10-12T00:00:00Z" }, 
            { "_id" : "5bc102fddff41a0e844dd2bb", "name" : "Meagan Shawn", "date" : "2018-10-12T00:00:00Z" }, 
            { "_id" : "5bc1044dbba8b31ed42e7408", "name" : "Stephen Stone", "date" : "2018-10-12T00:00:00Z" }
        ]
    }]
},
{ "_id" : "5bb39baf40f87f17f01734f8",
    "main_section" : {
        "dept" : "training"
    }, 
    "first_array" : [{ "_id" : "5bb39baf40f87f17f01734fc",  "floor" : "1st floor",
        "second_array" : [
            { "_id" : "5bc102f6dff41a0e844dd2b7", "name" : "Micheal Harrison", "date" : "2018-10-12T00:00:00Z" }, 
            { "_id" : "5bc102fddff41a0e844dd2bb", "name" : "Favour Reality", "date" : "2018-10-12T00:00:00Z" }, 
            { "_id" : "5bc1044dbba8b31ed42e7108", "name" : "Gift Myers", "date" : "2018-10-12T00:00:00Z" }, 
            { "_id" : "5bc1044dbba8b31ed42e71a1", "name" : "Drake Hills", "date" : "2018-10-12T00:00:00Z" }, 
            { "_id" : "5bc1044dbba8b31ed42e74c2", "name" : "Hashtan Priest", "date" : "2018-10-12T00:00:00Z" }
        ]
    }]
},
{ "_id" : "5b98987f08925c0f5cd86780", 
    "main_section" : {
        "dept" : "forecast"
    }, 
    "first_array" : [{ "_id" : "5b98987f08925c0f5cd86784", "floor" : "4th floor",
        "second_array" : [ ]
    }]
},
{ "_id" : "5b98187f08924c0f5ad86790", 
    "main_section" : {
        "dept" : "internal audit"
    }, 
    "first_array" : [{ "_id" : "5b98987f08925c0f5cd86784", "floor" : "4th floor",
        "second_array" : [ ] 
    }]
},
{ "_id" : "5b98187f08924c0f5ad86790", 
    "main_section" : {
        "dept" : "research"
    }, 
    "first_array" : [{ "_id" : "5b98987f08925c0f5cd86784", "floor" : "4th floor",
        "second_array" : [ ] 
    }]
}

基本上我希望做的是让每个数组产生元素总数 在数组中与主要部分并排第二 设计将输出3 训练将输出5 预测将输出0 内部审核将输出0 研究将输出0

因此,我们非常感谢您的帮助!

0 个答案:

没有答案