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?
2
Answers
No, you cannot combine
arrayContainsAny
andarrayContains
in the same query. According to the official documentation regarding array membership:To solve this, you should use one of the conditions in the query and filter the rest of the results on the client.
If the two Arrays are not too large (Maximum size for a document is 1 MiB) you could create a new field that combines both Arrays as follows:
and then use the following query:
Note that the arrays values should not contain the separator,
_
in this example.