I want to perform orderyBy "commentedBy" field and "geoHash" field together. Since firestore doesn't allow multiple orderBy on not equals operator. Is there any way to perform this query?
I'm trying to show all nearby posts which is not already commented by current user.
My code is below:
query
.collection("posts")
.orderBy("commentedBy")
.whereNotIn("commentedBy",listOf(FirebaseAuth.getInstance().currentUser!!.uid))
.whereEqualTo("disabled", false)
.whereEqualTo("expired", false)
.orderBy("geoHash")
.whereGreaterThanOrEqualTo("geoHash", boundingBox.minimumMatch)
.whereLessThanOrEqualTo("geoHash",boundingBox.maximumMatch)distanceForRadius)
The problem is not that multiple
orderBy
sortings are applied on a 'not equals' query. For example, this does work as a query:The problem with your query is that it's combining range clauses (
whereGreaterThanOrEqualTo
,whereLessThanOrEqualTo
) with an inequality clause (whereNotIn
) on different fields (geohash, uID). This is not supported as shown in the query limitations:So your query cannot be done as is. Something that you can try is shifting the filtering for "not the current user" from the query into a stream (the
getDocuments()
method supports Java streams):This worked on my tests with simple placeholder data for user IDs and geohashes (and after creating the relevant index as directed by the terminal output).