I used Mongo 3.2 on debian 8. Node 5.11.1 and Mongoose. The object are stored
{
"_id": {
"$oid": "5756e710eb8863716246689a"
},
"id": 735842,
"employee_id": 477,
"matricule": "1020510",
"name": "",
"date_day": "2016-04-24T22:00:00.000Z",
"morning_status": "P",
"morning_time_start": "08:16",
"morning_time_end": "12:12",
"afternoon_status": "P",
"afternoon_time_start": "12:57",
"afternoon_time_end": "18:30",
"working_time_theorical": 28800,
"working_time_employee": 34140,
"extra_hours": 5340,
"extra_hours_week": 10680,
"extra_hours_month": 78120,
"comments": ""
}
I want to extract the data with the field : date_day
var reporticoSchema = mongoose.Schema({
id :
{
type:Number,
index:true,
required:true
},
employee_id :
{
type:Number,
index:true,
required:true
},
matricule :
{
type:String,
index:true,
required:true
},
date_day :
{
type: Date,
index:true
},
.....
var Reportico=module.exports = mongoose.model('Reportico',reporticoSchema)
var dateStart=new Date('2016-04-01').toISOString(),
dateEnd=new Date('2016-04-30').toISOString();
console.log('d',dateStart,dateEnd)
var employeeId={employee_id:521,date_day:{$gte:dateStart,$lte:dateEnd}};
//console.log('employeeId',employeeId)
Reportico.find(employeeId).exec(function(err, employees) {
console.log('result')
if (err)return console.log('err',err);
console.log('employees',employees)
})
I updated the code to add te format of the field date_day The result is empty.
date_dayfield defined in your Mongoose schema, you seem to have skipped this crucial part? Again, if that's defined as theDatetype then there is no need to parse the JS Date object instance to a string, you can simply use raw JS Date instances in mongo queries without explicitly casting them toISOStringformat.toISOString()method on the date instances as that will parse them to strings, which is not necessary here i.e.var dateStart=new Date('2016-04-01'), dateEnd=new Date('2016-04-30'), query={employee_id:521,date_day:{$gte:dateStart,$lte:dateEnd}};Reportico.find(query).exec(...)