0

I need to filter a huge amount of data. an example I want to filter gyms from the firestore based on their provided amenities and their services provides=d. Both amenities and services are an array.

Query:

Select all gyms From the database Where Amenities[wifi, pool] and Services[parking, yoga]

How can I perform this on Firebase Firestore based Project?

gso_gabriel
  • 4,199
  • 1
  • 10
  • 22
Jithin Joy
  • 176
  • 2
  • 13
  • I know there is Array Contain Any can be used as an option. But we can use only one array-contains-any clause per query. – Jithin Joy Mar 31 '20 at 04:14

1 Answers1

1

Indeed, as you mentioned, it's not possible to use more than one array-contains-any per query. As mentioned by one of Firebase developers in this Github issue, this is the reason why it's not implemented:

We don't support this currently because we're worried about the combinatorial explosion of index rows required to support this kind of query. We're not planning on adding support for this any time soon.

Considering that, as alternatives for your usage, I have found this post from the Community here, that indicates two possibilities to substitute the querying with multiple arrays.

I think the one that you could try - as described in this answer - and that would fits you better, would be to preset first the values as properties, so you can use them in your code directly. The example of code would be something like the below.

    nAmenities = {
       "wifi": true,
       "pool": true,
       ...
    }

    nServices = {
       "parking" = true,
       "yoga" = true,
       ...
    }

    list.forEach( (val) => { 
        ref = ref.where(`nAmenities.${val}` , '==' , true);
        ref2 = ref2.where(`nServices.${val}` , '==' , true);

    //query of select using these above nAmenities and nServices variables

    });

This code is not tested, but I believe it might help you as a starting point. Besides that, I really encourage you to take a look at the other Stackoverflow Question, to verify all their clarifications and explanations.

Let me know if the information helped you!

gso_gabriel
  • 4,199
  • 1
  • 10
  • 22
  • dynamically creating Query? then how we can create the index of it? – Jithin Joy Apr 01 '20 at 12:08
  • Hi @JithinJoy yes, it's possible to index using dynamic queries. I would recommend you to take a look at these two other posts from the Community - [here](https://stackoverflow.com/questions/48386762/how-to-perform-dynamic-where-queries-with-firestore-and-add-indexes) and [here](https://stackoverflow.com/questions/49752379/firestore-index-for-user-created-fields) - where there are examples on how to achieve it. – gso_gabriel Apr 01 '20 at 14:03
  • those are not helping me brother – Jithin Joy Apr 02 '20 at 08:02
  • and Those are actually doing the AND operation right? – Jithin Joy Apr 02 '20 at 08:03
  • If I want to get all the glasses from the firestore as per the condition material : [ metal : true, acetate : true, mixed : fase ] shape : [ rectangle : true , square : false , round : true ] fit : [ narrow : true , medium : true , wide : false , extra-wide : false ] What I do? – Jithin Joy Apr 02 '20 at 08:14