Saturday 31 August 2019

How do i use dynamic query in mongoose

​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"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); How can I create a dynamic query, for instance, how do I get all document with "present" in the last 7 days? really wish someone will help out!

Submitted August 31, 2019 at 09:36AM by 2virtual

No comments:

Post a Comment