0

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) {
   }
});
almogbb
  • 29
  • 7

1 Answers1

0

There is no way to update an item in an array field by its index. Instead you'll have to:

  1. read the entire array into your application code
  2. update the array in memory
  3. 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:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807