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.