Martin
Martin

Reputation: 650

Firestore whereEqualsTo from arraylist

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

Simply:

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.

Upvotes: 2

Views: 1153

Answers (4)

Trevor Soare
Trevor Soare

Reputation: 120

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);

Upvotes: 0

user3103785
user3103785

Reputation: 1

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

Upvotes: 0

Martin
Martin

Reputation: 650

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

According to:

Upvotes: 0

Raj
Raj

Reputation: 3001

Try This

Query query = FirebaseFirestore.getInstance()
            .collection("posts")
            .whereEqualTo("tag", f.get(0)).orderBy("tag", Query.Direction.ASCENDING);;       

Upvotes: 1

Related Questions