Reputation: 1052
I 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[]","???");
Upvotes: 4
Views: 2889
Reputation: 138824
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.
Upvotes: 6