0

I'm currently struggling to delete an object/field inside a document in firebase. I'm able to delete a collection, or a full document, but not the name/tag fields for a given id. Does anyone know how I can delete the name and tags of a specific object inside the "info" array, for example Object#1 test2 imaged below? Any advice is greatly appreciated. Thank you!


const deleteCard = async (name, tags) => {
    const fieldRef = doc(db, 'labels', activeLabels)
    await updateDoc(fieldRef, {
      info: deleteField({name: name, tags: tags})
    })
    console.log(name, tags)
    getLabels();
  };

Firebase Array

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
jakeycodez
  • 49
  • 2
  • 7
  • In addition to Doug's answer, I think that this [resource](https://medium.com/firebase-tips-tricks/how-to-update-an-array-of-objects-in-firestore-cdb611a56073) will also help. – Alex Mamo Jul 29 '22 at 11:17

1 Answers1

1

It's not possible to delete an array item knowing only an object field value inside the array with a simple update command. The only way to make changes to an array like this is to read the document, modify the array contents in memory, then update the document field with the modified array.

It would be possible to delete the array item if you knew the entire contents of that array. You could use an arrayRemove operation for that, if you pass an object that is field-for-field equivalent with the item to delete.

See also:

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