Firestore query by date range

Nimer Farahty picture Nimer Farahty · Oct 29, 2017 · Viewed 82.5k times · Source

I need the help to query long collection with date range. See the below example document. I wanna query startTime field using date range.

enter image description here

enter image description here

Answer

Capy picture Capy · Oct 29, 2017

Since I have the dueDate field stored as "timestamp" (and NOT as string or number) on Cloud Firestore, I did this to get the invoice documents with a due date on 2017:

let start = new Date('2017-01-01');
let end = new Date('2018-01-01');

this.afs.collection('invoices', ref => ref
  .where('dueDate', '>', start)
  .where('dueDate', '<', end)
);

NOTE: dueDate field was stored at firebase with a Date() object. e.g.: this.doc.dueDate = new Date('2017-12-25')