2

I've a model like this

{
   "actions": [
      {
         "type": "1",
         "value": {
            "abc": {
               "1": "true"
            },
            "def": {
               "1": "true"
            }
         }
      }
   ]
}

I would like to delete abc field

Here's code im using but it did not work for me( Also i'm using batched writes to update multiple documents).

// getScene's data received from firestore
let batch = this.firebase.firestore().batch();
getScene.forEach(o => {
            let removeDeviceFromScene = this.sceneDoc.doc(o.id);
            const fieldPath = `actions.value.abc`;
            batch.update(removeDeviceFromScene, {
                    filePath: this.firebase.firestore.FieldValue.delete()
                }
            }); await batch.commit().then(() => {
            console.log(`Begin write batch`);
        });

i've also tried like this but same result

let update = {};
let removeDeviceFromScene = this.sceneDoc.doc(o.id);
update["actions.value.abc"] = this.firebase.firestore.FieldValue.delete();
batch.update(removeDeviceFromScene, update);

Is there something wrong with my code, any solution for this problem?

Thanks in advance

ThuongLe
  • 58
  • 4
  • It is not possible to update an individual element in an array. See https://stackoverflow.com/questions/51202300/how-to-add-update-remove-array-elements-in-firebase-firestore-android-using-hash – Frank van Puffelen Aug 26 '19 at 14:28

1 Answers1

0

What I said earlier is untrue. It is not possible to update an individual element in an array in a Firestore document. See How to Add/Update/Remove array elements in firebase firestore android using Hashmap? A Store Database

Since actions is an array, you'll need to specify the array index of the element you want to update. So:update["actions.0.value.abc"] = ...

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