I have my collection created as below:
-products
-productID
-category [object]
catitem1 - boolean
catitem2 - boolean
now I have written a query as below
this.afs.collection<Product>('products', ref =>
ref
.where(`category.${categoryName}`, '==', true)
.limit(limit)
);
This query works fine but when I add orderBy to the above query, I am asked to create an index for the query.
this.afs.collection<Product>('products', ref =>
ref
.where(`category.${categoryName}`, '==', true)
.orderBy('createdDate','desc')
.limit(limit)
);
Since the categoryName can be created and can be changed at anytime, I am supposed to add indexing for each and every categoryName which would be in hundreds.
Is there any way where I can create a wildcard index for category.categoryName?
I tried using category.* but that's not acceptable. Hope to find some help here.
Since this question was asked, Firestore has implemented various operators to help with querying for array membership. The available operators at time of writing are:
array-contains,array-contains-any,in, andnot-in.Instead of storing
categoryas anobjectwith aBooleanproperty for eachcategoryName, you could have acategoriesarray that only contains theStringnames (or, if you want to save space,Intids) of categories that the product is a member of.The query could then work like this, which would require only a single index: