Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore query by date range

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

like image 259
Nimer Farahty Avatar asked Oct 29 '17 13:10

Nimer Farahty


People also ask

How do you search on firestore?

Cloud Firestore doesn't support native indexing or search for text fields in documents. Additionally, downloading an entire collection to search for fields client-side isn't practical. To enable full text search of your Cloud Firestore data, use a dedicated third-party search service.

How do I get data from firestore collection?

There are two ways to retrieve data stored in Cloud Firestore. Either of these methods can be used with documents, collections of documents, or the results of queries: Call a method to get the data. Set a listener to receive data-change events.

How do I change firestore timestamp to date?

To convert a Firestore date or timestamp to a JavaScript Date, we use firebase. firestore. Timestamp. fromDate to convert the a date to a Firestore timestamp.


1 Answers

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')

like image 70
Capy Avatar answered Sep 18 '22 16:09

Capy