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

Does Firestore in Datastore mode support array-contains-any queries like Cloud Firestore?


Cloud Firestore has documentation for array-contains-any queries. However, the Firestore in Datastore mode documentation doesn't show any documentation for array-contains. It only has IN documentation.

My question is, does Firestore in Datastore mode support array contain queries too? Perhaps it does but the syntax is the same as for IN queries? Take this example:

Query<Entity> query =
    Query.newEntityQueryBuilder()
        .setKind("Task")
        .setFilter(PropertyFilter.in("tag", ListValue.of("learn", "study")))
        .build();

If the tag property is an array, will this return entities that where the array contains "learn" or "study"?

Similar to this example in Cloud Firestore?

CollectionReference tasksRef = db.collection("Task");
tasksRef.whereArrayContainsAny("tag", Arrays.asList("learn", "study"));

As a side question, are there three (3) Firestore products now? Cloud Firestore, Firestore in Native mode, Firestore in Datastore mode?


Solution

  • Are there three (3) Firestore products now? Cloud Firestore, Firestore in Native mode, Firestore in Datastore mode?

    No, there are only two. Cloud Firestore and Firestore in native mode are the same.

    Cloud Firestore has documentation for array-contains-any queries. However, the Firestore in Datastore mode documentation doesn't show any documentation for array-contains.

    Yes, Firestore provides indeed an array-contains-any query. However, you're asking if Firestore in Datastore mode contains an array-contains and not an array-contains-any query. There's a big difference between these queries. Please note that Firestore (in Native mode) provides both, array-contains-any and array-contains queries. On the other hand, Firestore in Datastore mode doesn't provide an array-contains-any query, but it offers the capability to search an array for a specific value using equality filters.

    If the tag property is an array, will these return entities where the array contains "learn" or "study"?

    If the "tag" field is a String, then it will check against the values in the list. If the "tag" field is an array, it will not check each element in the array against each element in the list. Most likely an Exception will be thrown.

    If you want to benefit from queries like array-contains, in, not-in, or array-contains-any, then I recommend you use Firestore (in Native mode).