You just need add a list field in the hashmap:
Map<String, Object> docData = new HashMap<>();
docData.put("favFoods", Arrays.asList("Hamburger", "Vegetables"));
db.collection("data").document("one")
.set(docData)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "DocumentSnapshot successfully written!");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error writing document", e);
}
});
Alternatively, you can also use arrayUnion
to add new items to an array.
DocumentReference docRef = db.collection("col").document("docId");
docRef.update("favFoods", FieldValue.arrayUnion("Pizza"));
You can add array of objects (and even nested maps) in Firestore. However, you should add the array as a list.
References: