2

I have a Firestore document that contains array like this:

Array:                    Array:
[1, 2, 3, 4, 5, 6, 7] or ['Zhangir', 'Erasyl', 'Arman']

And I need to check if this document contains any of given arrays. For Example:

[1, 2, 3, 4] or [1, 2, 7] or ['Zhangir', 'Arman'] and so on

and return it only if it has exact matches. But considering that my arrays can be 100 elements long, that would be very inconvenient to say arraycontains [1], arraycontains[2]... each time. Is there any way to do a compound query for a lot of values? All information that I found says I can't, but maybe there's a way.

From my example that would be

Firestore.instance.collection("Querys")
.where('array', arrayContains: [1, 2, 3, 4, 5]);

or

Firestore.instance.collection('Querys')
.where('array', arrayContains: ['Zhangir', 'Arman']);

and not something like

Firestore.instance.collection('Querys')
.where('array', arrayContains: 'Zhangir')
.where('array', arrayContains: 'Arman');
8
  • docRef.where("myArray", ">=", 1).where("myArray", "<=", 4); where docRef is the reference pointing to your document, and myArray is your array field within your document. More info can be found on the documentation for compound queries Commented Dec 2, 2019 at 10:02
  • @sloppis, it doesn't necessarily has to be 1, 2, 3, 4, it can be any given length and any given sequence, for example [3, 6] or [1, 2, 7] and so on. I updated the question Commented Dec 2, 2019 at 10:06
  • Hi @ZhangirSiranov, the previous solution would only work for a range of sequences, and you will need to create a composite index. On the other hand, for your current use case, I would suggest to use the array-contains-any operator to combine up to 10 array-contains clauses on the same field with a logical OR. More info can be found here. Notice that there is a small difference between in and array-contains-any operators, since in matches for an exact match of array length, order, and values. Commented Dec 2, 2019 at 10:38
  • @sllopis, but with that I will have to go through a lot of unwanted documents, increasing the size of the query. Commented Dec 2, 2019 at 10:41
  • @sllopis, because array-contains-any looks for only one element that satisfies the test Commented Dec 2, 2019 at 10:43

1 Answer 1

7

Firestore can now check for these condition on array fields:

  • Whether the field contains a specific value with array-contains.
  • Whether the field contains any of a list of (up to 10) values with array-contains-any.

What you're describing is an array-contains-all like operation, which currently doesn't exist. And you can't create it by combining multiple array-contains checks, as you can only have one of those in a query. You might want to file a feature request for it.

The best option today are to:

  1. Pick one value from the array, and perform an array-contains query with that value. Then you do the additional filtering in your application code.
  2. Use a map to store the values, and then use multiple where value == true conditions, as shown in the answer to this question: Firestore: Multiple 'array-contains'
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks sir, but I'm afraid that I will have to query a lot of documents If I do it this way. I'm going to go with different solution though, but still thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.