1

I have the following path in Firestore:

country/wakanda/cityID/12345ABCDE/storetime/123456789

  • country, cityID, and storetime are collections.
  • wakanda, 12345ABCDE, and 123456789 are documents.

Fields that are:

  • known before running the query: country, wakanda, cityID, storetime
  • unknown before running the query: 12345ABCDE, 123456789

What is the query for returning all values where storetime documents is greater than X and using a specifically known city. X is timestamp.

I was able to get snapshot containing all values where storetime is greater than X using this:

val storesResponse = firestore.collectionGroup("storetime").whereGreaterThan(xField, xInSec).get().await()

However, since collectionGroup is a query type, it can't be applied to a CollectionReference or a DocumentReference.

Is it possible to write this in a single query or am I approaching the problem wrongly?

Semi-Solution:

Thanks @Frank van Puffelen for your reply. I implemented the solution from the link you posted, and it does solve half of the problem which is filter for a specific city. However, I couldn't get to filter for storetime greater than X. I'm posting my solution below for future users looking for Android implementation:

val countryRef = firestore.collection("country").document("wakanda")

val storesAsync = firestore.collectionGroup("storetime")
                    .orderBy(FieldPath.documentId())
                    .startAt(countryRef.path + "/")
                    .endAt(countryRef.path + "\uf8ff").get()
                    .addOnSuccessListener {
                        Log.i(TAG, "Successfully read data, snapshot is: $it")
                    }.addOnFailureListener {
                        Log.i(TAG, "Failed to read data, exception is: $it")
                    }

            val storesResponse = storesAsync.await()

Now, if I tried to introduce the whereGreaterThan filter operator, it doesn't work.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Red M
  • 2,609
  • 3
  • 30
  • 50
  • 1
    Are you trying to query for documents in the `storetime` collections under a certain path only? If so, have a look at @samthecodingman's answer here: https://stackoverflow.com/questions/68049541/collectiongroupquery-but-limit-search-to-subcollections-under-a-particular-docum/68049847#68049847 – Frank van Puffelen Feb 02 '23 at 21:54
  • I'm confused on a certain part looking into that answer. For android/java/kotlin, should this: .orderBy(firebase.firestore.FieldPath.documentId()) be replaced by this: .orderBy(FieldPath.documentId()) or this: .orderBy(FieldPath.of("country" + "/" + "wakanda")) ??? – Red M Feb 03 '23 at 00:04
  • As the link shows, you should **order** on the `documentId()` and then filter on the prefix path that you want to match. If you're having trouble making that work on Android, I recommend posting a new question with a separate [minimal repro](http://stackoverflow.com/help/mcve) pointing to the question I linked as what you're trying to replicate on Android. – Frank van Puffelen Feb 03 '23 at 01:14
  • Hey there. Besides samthecodingman's answer, I think that this [resource](https://medium.com/firebase-developers/how-to-query-collections-in-firestore-under-a-certain-path-6a0d686cebd2) will also help. – Alex Mamo Feb 03 '23 at 06:41

0 Answers0