I have created an employee attendance application where attendances are logged and stored in a database. I have been able to obtain a count of the particular date-field with the value of “Present” How do i create a dynamic query to get the last 7 days record?
the code is written like so :
Employee.collection.countDocuments({"attendances.2019-08-26":"Present"},(err,data)=>
{ if(err){
res.status(500)
res.send(err)
}else{
res.status(200)
res.json(data)
}
})
// mongoDb database records
{
"_id": "5d6565236574b1162c349d8f",
"name": "Benjamin Hall",
"department": "IT",
"origin": "Texas",
"employDate": "2019-08-27",
"__v": 0,
"attendances": {
"2019-08-28": "Sick"
}
},
{
"_id": "5d6367ee5b78d30c74be90e6",
"name": "Joshua Jaccob",
"department": "Marketing",
"origin": "new york",
"employDate": "2019-08-26",
"__v": 0,
"attendances": {
"2019-08-26": "Present",
"2019-08-27": "Sick"
}
},
// output is 1
//schema
const Schema = mongoose.Schema;
const employeeSchema = new Schema({
name: String,
department: String,
origin: String,
employDate: String,
attendances: Object
});
module.exports= Employee = mongoose.model('Employee', employeeSchema);