4

I'm trying to query for a specifing date in Firestore. Until now I have tried this code:

let ref = db.collection('schools').doc('DglhflywuybkOuCq7tGW')
let start = new Date('2018-04-27')
let end = new Date('2018-04-28')
let reportRef = ref.collection('reportes').where('date', '>', start).where('date', '<', end)

In this code I need two variables, one for storing the start day (the one I'm looking) and the end, which is the start plus 1 day more. After that I query for the intersection of the two.

Is this the right way or there is a simpler way?

Javier Cárdenas
  • 3,935
  • 4
  • 25
  • 35
  • I think you approach is good. You could also just store the date, with no time, on the document and then query for the date. – DauleDK May 26 '18 at 20:42
  • How can I store a date with no time? – Javier Cárdenas May 26 '18 at 20:45
  • 1
    Are you using native js date, or a wrapper library like moment or luxon? If you use native js date, you can use the constructor like `new Date(year, month, day, hours, minutes, seconds, milliseconds)`, i.e provide 0 for hours, minutes, seconds and milliseconds. Then only the date is left. Then you can just query for this in firestore. – DauleDK May 26 '18 at 20:55
  • i'm using a wrapper library dayjs – Javier Cárdenas Feb 25 '19 at 16:29

0 Answers0