4

enter image description hereI want to query the highlighted values from the above database

My code is incomplete I can't figure how to complete the query. I want to know if 123,456,789 exists anywhere in the entire collection, like for a normal query .whereEqualTo("address","P18/A CIT Road Ananda Palit"); would give me the third document,here I want to query card

CollectionReference ref = db.collection("company");
Query query = ref.whereEqualTo("card[]","???");

1 Answer 1

6

To solve this, please use the following code:

ref.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        for (DocumentSnapshot document : task.getResult()) {
            List<String> list = (List<String>) document.get("card");
            for (String item : list) {
                    Log.d("TAG", item);
            }
        }
    }
});

The output will be:

123
456
789
//and so on

Remember, document.get("card") does not return an array, it returns an ArraList.

If you want use a Query using whereEqualTo() method then I suggest you change your database a little bit. Your database structure shoould look like this:

Firestore-root
   |
   --- company
         |
         --- companyDocumentId
                |
                --- address: "P18/A CIT Road Palit"
                |
                --- card
                     |
                     --- 123: true
                     |
                     --- 456: true
                     |
                     --- 789: true

In this case the your query should look like this:

Query = ref.whereEqualTo("card.123", true);

Edit:

According to your commend the query should be:

Query = ref.whereEqualTo("card.E20040806709002620760CE82", true);

Edit 13 Aug 2018:

According to the updated documentation regarding array membership, now it is possible to filter data based on array values using whereArrayContains() method. A simple example would be:

CollectionReference citiesRef = db.collection("cities");
citiesRef.whereArrayContains("regions", "west_coast");

This query returns every city document where the regions field is an array that contains west_coast. If the array has multiple instances of the value you query on, the document is included in the results only once.

Sign up to request clarification or add additional context in comments.

5 Comments

I want to query if 123,456,789 exists anywhere in the collection or not.. I am making a collection reference not a document reference. How do I do that.
Yes, I'm using CollectionReference. Although Cloud Firestore can store arrays, it does not support querying array members or updating single array elements. You cannot achieve what you want using your actual database structure. You can inseatd using mine. When you are looping through the list, see if a particular key exists, right?
Thanks I see, thank you so much alex. I will use your database structure. :)
Hey alex Query = ref.whereEqualTo("123", true); did not work, is that how we query within objects??
Thanks alex..I tried that and it didn't work for some time, turns out I had to trim the string the other function was returning. Thank a lot for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.