Reputation: 797
I am trying to have custom order to String collection elements in I fetch from Firebase FireStore.
I am quite confused with the documentation with custom indexes and composite indexes. I have several categories lets say Restaurants, Exits, Shops and I simply want them to be ordered the same way in when I fetch them. I want to receive Exits then Restaurants then Shops.
In the admin panel, they are not ordered properly and I would like to reorder them. The client code that fetches data is swift code.
Upvotes: 0
Views: 235
Reputation: 3149
Firestore automatically creates single-field indexes for all fields on your documents (read more). That means that simple queries will work straight out of the box.
So a query like this should provide you with the result that you are looking for: any place sorted by categories in alphabetical order. And this works because you are only querying one field.
placesCollectionRef.order(by: "category", descending: false)
But often you want to match one field and sort by another. And in that case you will need a composite index.
placesCollectionRef
.whereField("state", isEqualTo: "CA")
.order(by: "category", descending: false)
Firebase is being very helpful in identifying when we need an index. If you do a test run of your app, Firebase will throw an error if there was any query that was missing a necessary index. The error message will contain a URL that links to your Firebase project console and opens a dialog for creating the index.
Upvotes: 2