How to get an array from Firestore?
There are two solutions for your problem, one, you can cast the value from your document in the next way:
DocumentSnapshot document = task.getResult();
List<String> dungeonGroup = (List<String>) document.get("dungeon_group");
Or, and I would recommend you this solution because there is always a possibility that your model will change when you are developing your app . This solution is just model everything in Firebase POJO's even if they have just one parameter:
public class Dungeon {
@PropertyName("dungeon_group")
private List<String> dungeonGroup;
public Dungeon() {
// Must have a public no-argument constructor
}
// Initialize all fields of a dungeon
public Dungeon(List<String> dungeonGroup) {
this.dungeonGroup = dungeonGroup;
}
@PropertyName("dungeon_group")
public List<String> getDungeonGroup() {
return dungeonGroup;
}
@PropertyName("dungeon_group")
public void setDungeonGroup(List<String> dungeonGroup) {
this.dungeonGroup = dungeonGroup;
}
}
Remember that you can use the Annotation @PropertyName to avoid call your variables in the same way that your value in the database. Doing it in this way finally you can just do:
DocumentSnapshot document = task.getResult();
Dungeon dungeon= toObject(Dungeon.class);
Hope that it will help you! Happy coding!
If you want to get the entire dungeon_group
array you need to iterate over a Map
like this:
Map<String, Object> map = documentSnapshot.getData();
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getKey().equals("dungeon_group")) {
Log.d("TAG", entry.getValue().toString());
}
}
But note, even if the dungeon_group
object is stored in the database as an array, entry.getValue()
returns an ArrayList
and not an array.
A better approach for you would be if you consider this alternative database structure, in which each group
is the key in a Map
and all values are set to the boolean true
:
dungeon_group: {
3P: true,
Urgent: true,
Mission Chalange: true
//and so on
}
Using this structure you'll also be able to query it based on the property that exists within the dungeon_group
map, otherwise as in the official documentation:
Although Cloud Firestore can store arrays,
it does not support
querying array members or updating single array elements.
Edit 13 Jan 2021:
If instead of an array of String values you would have had an array of objects, then you can map that array of objects to a List of custom objects, as explained in the following article:
- How to map an array of objects from Cloud Firestore to a List of objects?
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.
When you call DocumentSnapshot.getData(), it returns a Map. You're just calling toString() on that map, which is going to give you a dump of all the data in the document, and that's not particularly helpful. You need to access the dungeon_group
field by name:
DocumentSnapshot document = task.getResult();
List<String> group = (List<String>) document.get("dungeon_group");
- edit: syntax error in typecasting