I have doc which has a date object.
Code to init Firestore:
FirebaseFirestore fireStore = FirebaseFirestore.getInstance();
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
.setTimestampsInSnapshotsEnabled(true)
.build();
fireStore.setFirestoreSettings(settings);
firestore = fireStore;
Code to query:
FirebaseFirestore db = FireStoreUtils.getInstance();
Query query= db.collection(viewType.collectionName());
query.whereLessThan("endDate", new Date());
return query.orderBy(viewType.sortProperty()).limit(PAGE_SIZE);
I am always getting all the records and looks like the where clause is not getting applied. On Firebase console I see that the endDate
is stored as timestamp.
Doc from Firebase console:
createdDate: null (null)
description: "desc" (string)
endDate: February 3, 2019 at 11:18:58 PM UTC-8 (timestamp)
id: "-7475596197450085332" (string)
title: "title"
I've faced the same problem and my solution comparing and filtering dates is this:
Note that firebase store
Date
asTimestamp
so instead of create aDate
to compare, I create aTimestamp
instead.Remember to import
Timestamp
from Firebase:Code to query:
I'm using firebase version:
I hope it helps!!
Cloud Firestore queries are immutable, which means that you cannot change the properties of an existing query. If you change the value by calling
whereLessThan()
method, it becomes a new query. So to solve this, please chain all method calls and store them in a newQuery
object like this: