1

I have created an application that stores attendance of employee in an organization. Each employee belonging to a different department. The challenge is, am trying to figure out how I can query MongoDB using mongoose such that I can obtain 7days worth of data of the total number that were present in a particular department.

I tried using the following, but am unable to figure out the query that will yield my desired result :

    const d =new Date(); 
    d.setDate(d.getDate()-7); ....//7days record 
    db.user.aggregate(["required query here"])... //record based on query

I haven't really got much experience in mongoose and Nodejs, just the basics.

Here is the stored info in my MongoDB database/collection


        {      //extracted from my mongoDB database

                "_id": "5d40c2a35a6da12cd8506fac",
                "name": "Jonhny Fredicks",
                "department": "Marketing", //department is first criteria
                "origin": "N/Y",
                "joinDate": "2019-07-30",
                "__v": 0,
                "attendances": {  
 // attendance is second criteria        
                    "2019-07-30": "Present",// total number of "present" is the final goal.This would be done for all employee with the same "department" field.
                    "2019-07-31": "Sick",
                    "2019-08-01": "Present",
                    "2019-08-02": "Present",
                    "2019-08-06": "Present",
                    "2019-08-08": "Vacation",
                    "2019-08-10": "Present",
                    "2019-08-12": "Sick",
                    "2019-08-21": "Present"
                }


I would really appreciate your suggestions: my goal is to be able to fetch those employees records, who were present in last 7 days, send it to the front-end where I will use that number to multiply their inputted hourly rate. hence compute their wages for the week.

4
  • You meant to say your goal is to "fetch those employees records, who were present in last 7 days"? Commented Aug 22, 2019 at 14:06
  • @Sumit Vekariya Yes, I have edited the question it to mean just that. Thanks Commented Aug 22, 2019 at 14:08
  • @8SINS : Is attendances an array or object itself ? Also do you need how many days each employee has present in past 7 days, Do you want object to be returned or day count ? Commented Aug 22, 2019 at 15:22
  • I have already figured out the way to calculate the number of times each employee was present in the last 7days(I have each result separately), what I want is the total "present" count in the last 7days of all employee of "department": "X". I guess "attendances" should be an object Commented Aug 22, 2019 at 15:42

1 Answer 1

0

Follow that hope you will able to find out the employee who are working for last 7 days out of 9 days.

router.get("/api",(req,res,next)=>{
      db.find().then((result)=>{
      var js=[];
      for(var a = 0;a<result.length;a++){
        obj=result[a];
        var counter =0;
        var name= Object.getOwnPropertyNames(obj.attendances);
        for(var i=0;i<name.length;i++){
          if(obj.attendances[name[i]]=="Present"){
            counter +=1;
          }
        }
        if(counter>=7){
          js.push(obj);
          console.log("Okay")
        }else{
          console.log("It's not okay")
        }
       }
       console.log(js);
        res.json({data:js});
       });
     });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.