firebasegoogle-cloud-platformgoogle-cloud-firestoregoogle-cloud-datastore

How does multiple equality filters on an array field in Firestore (in Datastore mode) work?


The Firestore (in Datastore mode) documentation says:

Datastore mode indexes each unique array property value once per index. Thus to query if an array contains a value use an equality filter.

This statement implies that you can only query for one value with an equality filter. However, there is also this example, with the following note that says:

Unlike inequality filters, multiple equality filters can be used to query for entities that contain a set of values.

Query<Entity> query =
        Query.newEntityQueryBuilder()
            .setKind("Task")
            .setFilter(
                CompositeFilter.and(
                    PropertyFilter.eq("tag", "fun"), PropertyFilter.eq("tag", "programming")))
            .build();

I'm confused about what this query can do. Does this mean that this query will:

a) Return Task entities where the "tag" field is an array and the array ONLY contains the values "fun" and "programming"

OR/AND

b) Return Task entities where the "tag" field is an array with many different values (e.g. ["fun", "programming", "algo", "interview"]), but at least two of them are "fun" and "programming"?

The example from the documentation appears to be similar to the array-contains-any example from Firestore, but it isn't clear as to what its limits/abilities are.


Solution

  • The code you provided in your question uses two equality filters. So the query is searching for entities that have the values fun and programming in the tag array property. Remember, the CompositeFilter.and() method combines the two PropertyFilter.eq() filters into a single filter that requires both conditions to be met. So it's an AND operation and not an OR. However, the tag can contain multiple values, not only those two.