0

I'm trying to delete a single element of an Firestore array. The database has the following structure:

enter image description here

Unfortunately, with the following code nothing happens.

public static void deleteTable(final int pos){
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    FirebaseAuth fAuth = FirebaseAuth.getInstance();

    DocumentReference docRef = db.collection("users").document(fAuth.getUid().toString());
    docRef.update("myTables", FieldValue.arrayRemove(pos));
}

It would be nice if someone could help me out with this one. I just want to delete one element out of the myTables array. (The elements are HashMaps)

Cheers Andi

andyyy
  • 23
  • 3

1 Answers1

1

Firestore does not support modification of array fields by index. FieldValue.arrayRemove() must always be delivered the full contents of the item to remove. In your case, that would be a Map containing all of the nested fields in the item to remove.

If you want to remove a field by index, you have to read the document, modify the array in memory, then write the modified array back to the document.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441