I was building a simple query that includes ordering at the end.
But firestore is asking me to create index that combines the queried field and the orderBy
field. And it is going to be many indexes that will be created for this that combine the order field with the rest fields (or even combination of fields).
I have 4 fields to filter results by, and need to be ordered by another field for which I have to create 24 indexes for the different combinations, including both ascending and descending for order. This way if I have tens of fields? I think I am going to have to create hundreds of indexes.
So is it a good practice to get on and create all those numerous indexes of combination of fields combined with the order by field, even for both directions ASC
and DESC
? would it create issues if I proceed with that? or what pattern is fit for this simple task way to do this?
My query is
var query = ref.collection("data/devices");
if (params.region) query = query.where("region", "==", params.region);
if (params.color) query = query.where("color", "==", params.color);
query = query.orderBy("price", "ASC ");
in my test, only params.color
wasn't included since it was undefined. So the query only had .where("region")
and orderBy price
And it generated this error Error: 9 FAILED_PRECONDITION: The query requires an index. You can create it here: [Link to create the index]