Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Query Firestore for documents that satisfy AND condition

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

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-contains clause per disjunction (or group). You can’t combine array-contains with array-contains-any in 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading