Solution 1 :
There is no way to update an item in an array field by its index. Instead you’ll have to:
- read the entire array into your application code
- update the array in memory
- write the entire updated array back to the database
The exception to this is when your array contains unique, unordered values, such as when you for example store an array of the tags for a post. In that case you can use the arrayUnion
operator to manipulate the array. However your case looks different, so you’ll have to use the above steps.
This questions comes up quite regularly, so I recommend reading some of these:
Problem :
I’m trying to update some data in my firestore database and the data inside is Map<String,List<String>>
in other words, a map of lists.
so when I’m trying to update a list inside the map, it casting the list to a map.
my code:
Calendar calendar = Calendar.getInstance();
String currentDate = DateFormat.getDateInstance().format(calendar.getTime());
DocumentReference docRef = db.collection("incomes").
document(firebaseAuth.getCurrentUser().getUid());
docRef.update(
index + ".0", incomeTitleEt.getText().toString(),
index + ".1", incomeAmountEt.getText().toString(),
Integer.toString(index)+".2",currentDate
).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
}
});
Comments
Comment posted by almogbb
You mean take it from the database, then update it and send it back to the database?
Comment posted by Frank van Puffelen
Yes, that’s what it takes to update an item in the databae.