0

So I'm struggling to get the following query to work:

firebaseFirestore.collection("kids")
            .document(documentID)
            .collection("books")
            .whereEqualTo("accountID", accountID)
            .whereEqualTo("checkoutDate", epochDate)
            .whereGreaterThanOrEqualTo("checkoutDate", firstDayThisWeekDate)
            .orderBy("checkoutDate")
            .orderBy("startTime", Query.Direction.ASCENDING)
            .orderBy("lengthOfTime", Query.Direction.DESCENDING);

epochDate is a date object set to 01/01/1970 firstDayThisWeekDate is a date object that refers to the calendar date of the first day of this week (Sunday).

If I only have one of the two checkoutDate equalities the query works. But if I combine the two I get zero results. What am I missing?

This Works!

firebaseFirestore.collection("kids")
            .document(documentID)
            .collection("books")
            .whereEqualTo("accountID", accountID)
            .whereEqualTo("checkoutDate", epochDate)
            .orderBy("checkoutDate")
            .orderBy("startTime", Query.Direction.ASCENDING)
            .orderBy("lengthOfTime", Query.Direction.DESCENDING);

This Works!

firebaseFirestore.collection("kids")
            .document(documentID)
            .collection("books")
            .whereEqualTo("accountID", accountID)
            .whereGreaterThanOrEqualTo("checkoutDate", firstDayThisWeekDate)
            .orderBy("checkoutDate")
            .orderBy("startTime", Query.Direction.ASCENDING)
            .orderBy("lengthOfTime", Query.Direction.DESCENDING);

Here's the scenarios that I have for the books documents. If the field: "repeats" is true, the checkoutDate will ALWAYS be the epochDate If the field: "repeats" is false, then the dates will be something greater than the epochDate.

My goal is to be able to grab the epochDate documents and any documents that have a date that is on or after the first day of this week.

I'm trying hard to accomplish this within one query, and I think I can. I just can't reconcile having the two date comparisons. It seems like the docs say you can.

Any help would be greatly appreciated.

Brian Begun
  • 509
  • 7
  • 24
  • What are you hoping to get by adding `.whereEqualTo("checkoutDate", epochDate) whereGreaterThanOrEqualTo("checkoutDate", firstDayThisWeekDate) ` in the same query? That greaterThanOrEqualTo clause is kinda meaningless, since you're already searching for `whereEqualTo` right above. – Todd Kerpelman Oct 14 '20 at 17:44
  • Hi Todd. Thanks so much for the response. The goal of this query is to get documents that contain the epoch date AND documents that exist on or after the first day of this week. All of the dates should be excluded. If there is a way to do it with the combination of "repeats" field, I am also okay with that – Brian Begun Oct 14 '20 at 18:09
  • As I mentioned above the epoch date is always associated with repeats == true, and I would always like to return those documents along with documents that are from the first day of this week. So Any combination of the two date equalities or repeats equals true and the current week + forward dates would be acceptable. – Brian Begun Oct 14 '20 at 18:10
  • What you're basically describing is an OR query, which isn't really something that Cloud Firestore supports. I think you're probably better off breaking this up into two separate queries. – Todd Kerpelman Oct 14 '20 at 18:49
  • Yeah, I was so hoping to avoid breaking it up into two queries.. No other options huh? – Brian Begun Oct 14 '20 at 18:54
  • Does this answer your question? [Firebase Firestore - OR query alternative](https://stackoverflow.com/questions/46805749/firebase-firestore-or-query-alternative) – Kato Oct 15 '20 at 17:58
  • Thanks Kato for the info. I don't think it actually helps though. I was originally downloading all my documents, but I'm now processing the expired documents in a Cloud Function, I wanted to avoid downloading them unnecessarily. I think I'll have to opt for two separate queries and combine them. – Brian Begun Oct 16 '20 at 15:46

0 Answers0