2

I'm trying to list documents that matches field String value from ArrayList.

Simply:

  • I have ArrayList with tags stored at runtime
  • and documents with field tag

and I want to query documents that matches tag with one of tags stored in ArrayList. Is this possible with official query or does I have to download all documents and filter it client-side? Thanks for any answers.

Also, this is my method generating query:

public static Query getQueryForFollowed(DocumentSnapshot snapshots) {
    if (snapshots == null || !snapshots.exists()) {
        return FirebaseFirestore.getInstance().collection("posts").whereEqualTo("null", "null"); // return query that will get nothing
    }
    ArrayList<String> f = processFollowedTags(snapshots);
    Query query = FirebaseFirestore.getInstance()
            .collection("posts")
            .whereEqualTo("tag", f.get(0));
    for (int i = 1; i < f.size(); i++) {
        query = query.whereEqualTo("tag", f.get(i));
    }
    return query;
}

I have debugged code and query has contained requested conditions, but query didn't found any document matching it.

Martin
  • 652
  • 9
  • 27

4 Answers4

1

Try This

Query query = FirebaseFirestore.getInstance()
            .collection("posts")
            .whereEqualTo("tag", f.get(0)).orderBy("tag", Query.Direction.ASCENDING);;       
Raj
  • 2,997
  • 2
  • 12
  • 30
  • com.google.android.gms.tasks.RuntimeExecutionException: com.google.firebase.firestore.FirebaseFirestoreException: INVALID_ARGUMENT: Order by clause cannot contain a field with an equality filter tag – Martin Jun 29 '18 at 15:34
0

After some more search on Google I have found that querying field to multiple values is not available.

According to:

Martin
  • 652
  • 9
  • 27
0

Below code snippet may help you.

fun arrayContainsQueries() {
    // [START array_contains_filter]
    val citiesRef = db.collection("cities")
    citiesRef.whereArrayContains("regions", "west_coast")
    // [END array_contains_filter]

}

ref : git

0

As of Nov 2019 this is now possible to do with the in query.

With the in query, you can query a specific field for multiple values (up to 10) in a single query. You do this by passing a list containing all the values you want to search for, and Cloud Firestore will match any document whose field equals one of those values.

it would look like this:

 Query query = FirebaseFirestore.getInstance()
    .collection("posts")
    .whereIn("tag", f);
Trevor Soare
  • 120
  • 8