0

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.

4
  • 1
    How is the date_day field defined in your Mongoose schema, you seem to have skipped this crucial part? Again, if that's defined as the Date type 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 to ISOString format. Commented Jun 7, 2016 at 18:14
  • Yes the format of the field is date. Can you more explicit with a sample . Tks. Commented Jun 8, 2016 at 6:01
  • If that's the case then all you just need to do is remove the 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(...) Commented Jun 8, 2016 at 6:32
  • I tried too this. In Mongo, i type directly the same query and it have rows. Here the query don't return datas. Commented Jun 8, 2016 at 7:14

1 Answer 1

1

This is my consult with date, I using library http://momentjs.com/ to separate date

var searchParams = {};
var di = moment('2016-04-01', "YYYY-MM-DD").toArray();
var de = moment('2016-04-30', "YYYY-MM-DD").toArray();
searchParams['date_day'] = { $gte: new Date(di[0],di[1],di[2]), $lte:  new Date(de[0],de[1],de[2]) };
searchParams['employee_id'] = 521

Reportico.find(searchParams).exec(function(err, employees) {
     console.log('result')
     if (err)return console.log('err',err);
     console.log('employees',employees)
})
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, the result is the same: { date_day: { '$gte': Fri Apr 01 2016 00:00:00 GMT+0200 (CEST), '$lte': Sat Apr 30 2016 00:00:00 GMT+0200 (CEST) }, employee_id: 521 } --> employees [] And here the format is : Fri Apr 01 2016 00:00:00 GMT+0200 (CEST) . In Mongo the format is "2016-04-24T22:00:00.000Z"
Your problem is you need to format the date var di = moment(YOUR_DATETIME_START).toArray(); var de = moment(YOUR_DATETIME_END).toArray();

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.