loopback icon indicating copy to clipboard operation
loopback copied to clipboard

compare date object

Open HolaMan opened this issue 12 years ago • 17 comments

I try the Restful API with filter parameters below filter={ "where": { "date": {"lte": "2014-06-05"} }} But I can't find any record. I have tried another field with int type filter={ "where": { "price": {"lte": 123} }} I am wondering if gt, get, lt, lte only works for int, float, not work for date type

Any suggestion?

HolaMan avatar Jun 05 '14 14:06 HolaMan

Can you try IOS format, such as '2013-10-15T21:34:50.000Z'?

raymondfeng avatar Jun 05 '14 14:06 raymondfeng

I have tried also, but it still not work. I also try the code form. HistoryPrice.find({ where: { date: {gt: new Date('2013-06-05T21:34:50.000Z')}},
}, console.log ); And it doesn't work.

HolaMan avatar Jun 05 '14 15:06 HolaMan

what connector do you use?

raymondfeng avatar Jun 05 '14 15:06 raymondfeng

I just use default connector, i.e. memory-connector

HolaMan avatar Jun 05 '14 15:06 HolaMan

The memory connector only allows number comparison at this moment. We'll add support for other types.

raymondfeng avatar Jun 06 '14 06:06 raymondfeng

Hi Raymond Thanks for your quick reply. Might I know the which connector will support Date comparison, such as MongoDB, MySQL, or ....?

HolaMan avatar Jun 06 '14 06:06 HolaMan

Real DBs should support date comparison.

raymondfeng avatar Jun 06 '14 06:06 raymondfeng

Thanks, I have a small patch to support it if it's okay, I have tested it and it works fine. https://github.com/strongloop/loopback-datasource-juggler/pull/131 Thanks

HolaMan avatar Jun 06 '14 09:06 HolaMan

Fixed by https://github.com/strongloop/loopback-datasource-juggler/pull/132

raymondfeng avatar Jul 10 '14 04:07 raymondfeng

I'm having the same problem using the loopback-connector-mongodb 1.13.0.

Not work

{
  "where": {
    "status.created_at": {
      "gte": "2015-09-13T15:30.000Z",
      "lte": "2015-09-15T15:45.000Z"
    }
  }
}

Work

{
  "where": {
    "status.timestamp_ms": {
      "gte": 1442169000000, 
      "lte": 1442342700000 
    } 
  } 
}

I believe that is related to this issues #123 and #517 too

gustavorps avatar Sep 15 '15 17:09 gustavorps

I had to use "and" operation to separate the lte and gte comparisons. If I use them together inside the same { } doesnt work.

Doesn't work: {"where": { "epoch_time": {"lte":1459407675, "gte":1450717674} } },

Works: {"where": {and: [{"epoch_time": {"gte":1450717674}},{"epoch_time": {"lte":1459407675}}]} }

cchacons avatar Dec 30 '15 07:12 cchacons

I am also facing the same issue. @cchacons Solution worked :+1:

vasanthps-rm avatar Jan 19 '16 13:01 vasanthps-rm

Not sure if your issue is the same as mine was, my query also was failing on a Date field (using MongoDB).

I describe my issue and my solution here: Post to LoopbackJS Google group

Basically, it boils down to not having explicitly assigned my related model field to be of type 'Date'.

Hope it helps.

spirospolitis avatar Jan 20 '16 14:01 spirospolitis

@raymondfeng @cchacons any idea why this wouldn't be working for me? please help postgres-connector

Doesn't work (returns empty array)

const moment = require( 'moment' );
var
  now = moment(),
  yesterday = now.subtract( 24, 'hours' );

   Model.find({
      where: {
        and: [
          { expiresOn: { gt: yesterday.toJSON() }},
          { expiresOn: { lte: now.toJSON() }}
        ]
      }
    });

works (same upper bound so i know there is data i should be getting from the first query)

const moment = require( 'moment' );
var
  now = moment(),
  yesterday = now.subtract( 24, 'hours' );

   Model.find({
      where: {
          expiresOn: { gt: yesterday.toJSON() }
      }
    });

dancingshell avatar Nov 11 '16 03:11 dancingshell

Nevermind I was having a moment with moment.

... 
now = moment(),
yesterday = moment( now ).subtract( 24, 'hours' ); // you have to create a new moment instance to get yesterday, otherwise now and yesterday will be the same ( resulting in empty data :x)
...

dancingshell avatar Nov 11 '16 03:11 dancingshell

Thanks @spirospolitis !

TrevorPage avatar Aug 24 '17 23:08 TrevorPage

I also faced the same issue. Thanks to @cchacons for the solution. But it must be mentioned in official docs.

uds214125 avatar Feb 21 '18 13:02 uds214125