Home > Mobile >  Firestore: How to order results by not filtered field?
Firestore: How to order results by not filtered field?

Time:03-09

I saw the Firestore documentation and I need exactly a feature, that is not possible. I want to filter comments from a specific user and want to order his comments by date.

Like below (which is invalid according to Firestore):

docRef
    .whereField("user", isEqualTo: "John Doe")
    .order(by: "date")

How could I achieve this?

CodePudding user response:

You have to create an Index before you sort by another field than filtered. Go to Cloud Firestore -> Indexes to create it or follow the link provided in the error log.

CodePudding user response:

Firestore order and filter in multiple fields within these limitations. The combination you're showing in your code does not hit those limitations as far as I can see, so it should be possible. Most likely you're just missing the necessary index.

If you want to filter/order on multiple fields, you'll need to define an index on that combination of fields. When the necessary index is missing, the output of your app will contain a message to that effect, which contains a direct link that you can click to create the index with all necessary fields prepopulated. So check the log output of your app for such a message whenever you're not getting the results you expect.

  • Related