Assuming I have documents in the Firestore in the following format:
{
'field1': ['v1', 'v2', 'v3'],
'field2': ['v5', 'v6']
}
I want to query for the documents where the field1 contains a value from the given values, and the field2 contains a given value. I want to perform an AND operation where I specify these conditions in a single query only and get all the valid documents. Following is what I tried:
await FirebaseFirestore.instance
.collection(COLLECTION)
.where("field1", arrayContainsAny: ['v1', 'v2'])
.where('field2', arrayContains: 'v1')
.get();
But I got an exception:
So Is there any way to combine these to make a query?
>Solution :
So is there any way to combine these to make a query?
No, you cannot combine arrayContainsAny and arrayContains in the same query. According to the official documentation regarding array membership:
You can use at most one
array-containsclause per disjunction (or group). You can’t combinearray-containswitharray-contains-anyin the same disjunction.
To solve this, you should use one of the conditions in the query and filter the rest of the results on the client.
