我创建了一个应用程序,用于存储组织中员工的出勤情况。每个员工都属于不同的部门。面临的挑战是,试图弄清楚如何使用猫鼬查询MongoDB,以便可以获取特定部门中存在的总数的7天数据。
我尝试使用以下内容,但无法找出产生所需结果的查询:
const d =new Date();
d.setDate(d.getDate()-7); ....//7days record
db.user.aggregate(["required query here"])... //record based on query
我对猫鼬和Nodejs并没有太多的经验,只是基础知识。
这是我MongoDB数据库/集合中存储的信息
{ //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"
}
我真的很感谢您的建议:我的目标是能够获取最近7天出现的那些员工记录,并将其发送到前端,在这里我将使用该数字乘以他们输入的小时费率。因此计算他们一周的工资。
答案 0 :(得分:0)
希望您能在9天内找到最近工作7天的员工。
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});
});
});