计算猫鼬node.js数组中的项目

时间:2018-10-18 20:00:09

标签: arrays mongoose count items

我想计算数组中的项目数。这是我的模特 我尝试了很多事情

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

我希望某些结果会产生3、2等,但是对于所有结果,它都会带来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
        }]
    }]
});

0 个答案:

没有答案