Skip to content

FireStore date query not working as expected

I have doc which has a date object.

Code to init Firestore:

 FirebaseFirestore fireStore = FirebaseFirestore.getInstance();
        FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
        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" 



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 new Query object like this:

Query query = db.collection(viewType.collectionName())
    .whereLessThan("endDate", new Date());
return query;
1 People found this is helpful