1

We are using firebase realtime db and have orders data. the date is stored as a string value and we are looking to query like orders in last one week. The data look like

"orders" : {
  "-LM_h6nKUKQY0wyqSCz4" : {
    "order" : {
      "_couponDiscount" : "382.5",
      "_date" : "17-9-2018",
      "_orderNumber" : "MannuSharma-21581",
      "_orderStatus" : "Delivered",
      "_orderTotal" : 450,
    }
  },
  "-LMatbq6prD4kNtTqzmx" : {
    "order" : {
      "_date" : "19-9-2018",
      "_orderNumber" : "AnkushVerma-22991",
      "_orderStatus" : "Delivered",
      "_orderTotal" : 600,
    }
  },
  "-LMaxcGocVxe9nnfFnT2" : {
    "order" : {
      "_date" : "18-9-2018",
      "_orderNumber" : "MannuSharma-40284",
      "_orderStatus" : "Delivered",
      "_orderTotal" : 450,
    }
  }

The _date is the field and in this case lets say i need to get data fro date 17 and 18th.

the startAt and endAt will do it lexographically it seems, so how do i get this data treated as date?

Vik
  • 8,721
  • 27
  • 83
  • 168
  • 2
    In addition to Renaud's answer, see https://stackoverflow.com/questions/40203540/firebase-endat-not-working-with-date-string, https://stackoverflow.com/questions/40203540/firebase-endat-not-working-with-date-string, – Frank van Puffelen Sep 29 '18 at 14:43
  • is it advisable way to query data lets say for last 6 months? we are looking at it to generate sales charts by days, weeks, months etc. – Vik Sep 30 '18 at 00:57
  • 1
    You'll quickly find that the querying capabilities of Firebase (and most other NoSQL databases) are quite limited for such use-cases. It is more common to keep running totals for all the data in your reports. So: as you write an order, also update the totals for the week, month, etc that the order belongs to. That way showing a report is incredibly simple. – Frank van Puffelen Sep 30 '18 at 14:19

1 Answers1

4

If you want to sort your dates lexicographically in such a way you can query from startAt to endAt you should store them as YYYY-MM-DD instead of DD-MM-YYYY.

Note that changing 19-9-2018 to 2018-9-19 would not work, you must store it as 2018-09-19.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121