-1

I have a bit of trouble getting data from mongodb. I have a collection like this:

    Activities = [
          ... some another activity object item ... ,

          {
              type: "special"
              dates: {
                  begin: ISODate("2019-07-07T17:00:00.000Z"),
                  end: ISODate("2019-07-20T17:00:00.000Z"),
                  note: ""
              },
              status: "pending"
          } ,
          ,
          {
              type: "event"
              dates: {
                times: {
                    from: "12:00",
                    to: "15:00"
                  },
                  begin: ISODate("2019-07-21T17:00:00.000Z"),
                  end: ISODate("2019-08-29T17:00:00.000Z"),
                  note: ""
              },
              status: "pending"
          } ,

          ... some another activity object item ...
      ]

I want to get all activity data has : dates.begin < X (my params date) < dates.end . And this is my way i have tried :

_Activities.find({
        "dates.end" : {
            $lte : "2019-08-31T17:00:00.000Z"
        },
        "dates.begin" : {
            $gte : "2019-07-15T17:00:00.000Z"
        }
    }, callback);
// return [];


_Activities.find({
        "dates.end" : {
            $lte : "2019-08-30T17:00:00.000Z"
        }
    }, callback);
// return [] too;

I don't know where I was wrong, anyone has a solution to help me :((( Thanks for taking the time .

hieu nho
  • 3
  • 2

2 Answers2

0

Because your fields have type Date so you need to pass a Date to compare. Something like this may work:

$lte : new Date("2019-08-31T17:00:00.000Z")
Cuong Le Ngoc
  • 11,595
  • 2
  • 17
  • 39
0

You can pass dates as javascript Date object it will work

dates: {
  end: { $lte: new Date('2019-08-31T17:00:00.000Z') } ,
  begin: { $gte: new Date('2019-07-15T17:00:00.000Z') }
}
Yasser Mas
  • 1,652
  • 1
  • 10
  • 14
  • Dear friend , thanks you for your solution , i tried this way but is still return a null array , maybe mongoosejs do not support this case , but we can use like 'dates.begin' : { $gte: new Date('2019-07-15T17:00:00.000Z') } and 'dates.end' : { $gte: new Date('2019-07-15T17:00:00.000Z') } . Thanks u a lot for taking time ;) – hieu nho Aug 23 '19 at 02:46
  • Sorry this is why it not working with you https://stackoverflow.com/a/16002726/6113960 – Yasser Mas Aug 23 '19 at 03:03