Home > database >  react-native firestore: Query with where() and orderBy() using different fields
react-native firestore: Query with where() and orderBy() using different fields

Time:09-14

I try to access data from my database using the following query:

await firestore()
  .collection('col')
  .where('nameLowerCase', '>=', searchString.toLowerCase())
  .where('nameLowerCase', '<=', searchString.toLowerCase()   '\uf8ff')
  .orderBy('nameLowerCase')
  .orderBy('liked', 'desc')
  .startAt(latest || '')
  .limit(15)
  .get();

I created the index for it, vut it the result isn't sorted by likes (It is sorted by names).

I tried some other solutions from kind of similar questions. With their help I got to the solution above. It doesn't log a warning or an error but isn't sorted by both orderBy().

CodePudding user response:

Since your query first orders by nameLowerCase and only then orders by liked, the result will only be ordered on liked in cases where multiple documents have the same nameLowerCase.

There is no way to change this in the query, so you will have to re-sort the results in your application code after getting them from the database.

  • Related