'Firestore: How to order results by not filtered field?
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?
Solution 1:[1]
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.
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Intra Preneur |
| Solution 2 | Frank van Puffelen |
