arindam89
6/28/2017 - 7:45 PM

Simple Mongo Query for DashBoard

Simple Mongo Query for DashBoard

var campuses_promise = campus.find().exec();
return campuses_promise.then(function(campuses){
   var allPromises = []; 
    _.forEach(campuses, (campus) => {
        
        allPromises.push(

        visitor.count({campusId: campus._id}).exec().then((count) => {
            campus.visitor_count = count;
        }),
        
        user.count({'ownership.campusId': campus._id}).exec().then((count) => {
            campus.user_count = count;
        }),
        
        employee.count({campusId : campus._id}).exec().then((count) => {
            campus.employee_count = count;
        }),
        
        userinapp.find({}).populate({path: 'userId', 
            model: 'user', select: 'ownership.campusId', 
            match: {'ownership.campusId': campus._id}})
            .count()
            .exec()
            .then((count) => {
                campus.user_in_app_count = count;
            }),
        
        attendance.find({}).populate({path: 
            'employeeId', model: 'employee', select: 'campusId', 
            match: {campusId: campus._id}})
            .count()
            .exec()
            .then((count) => {
                campus.employee_attendance = count;
            })
        )

    });

    return Promise.all(allPromises).then(() => { return campuses });
});