Firestore - Delete a field inside an object

18,555

Solution 1

The "dot notation" with the special "FieldValue.delete()" should work.

Try this:

    Map<String, Object> deleteSong = new HashMap<>();
    deleteSong.put("songList.songName3", FieldValue.delete());

    FirebaseFirestore.getInstance()
        .collection("yourCollection")
        .document("yourDocument")
        .update(deleteSong);

It worked for me.

See: https://firebase.google.com/docs/firestore/manage-data/delete-data https://firebase.google.com/docs/firestore/manage-data/add-data

Solution 2

This work for me in general

firebase
  .firestore()
  .collection('collection-name')
  .doc('doc-id')
  .set({ songlist : {
    [songName]: firebase.firestore.FieldValue.delete()
  }
  }, { merge: true });

Solution 3

Found this topic today and want to add my solution. I am using the dot notation. The following will remove the specific song from the songlist by using firestore.FieldValue.delete();. I am using the Node.js firebase-admin package written in TypeScript:

import * as admin from 'firebase-admin';
export async function removeSong(trackId: string, song: string) {
    try {
        const updates = {};
        // Dot Notation - will delete the specific song from songList
        updates[`songList.${song}`] = admin.firestore.FieldValue.delete();
        // Not necessary, but it's always a good practice
        updates['updatedAt'] = admin.firestore.FieldValue.serverTimestamp();

        await firestore.collection('songs').doc(trackId).update(updates);
        return true
    } catch (error) {
        return null;
    }
}

Solution 4

It did not allow me to comment above, but Manuel Borja's solution works and is pretty clean. Here is how I used it:

db.collection("coolcollection")
  .doc(id)
  .set(
    {
      users: {
        [firebase.auth().currentUser
          .email]: firebase.firestore.FieldValue.delete(),
      },
    },
    { merge: true }
  );

A few things to add: this works when the given field of the map exists and when it does not exist.

Solution 5

I had the same issue from Android using Kotlin, and I solved it with dot notation as well. For Android Kotlin users, that is:

 val songName: String = "songName3"
 val updatesMap = HashMap<String, Any>()
 updatesMap["songList.${songName}"] = FieldValue.delete()
 FirebaseFirestore.getInstance()
                  .collection("Your Collection")
                  .document("Your Document")
                  .update(updatesMap)

Hope this helps!

Share:
18,555
Gio
Author by

Gio

Updated on June 02, 2022

Comments

  • Gio
    Gio about 2 years

    I'm using Firestore and I would like to delete a field that is in a specific object. I can delete a field in a document thanks to :

    fieldName: firebase.firestore.FieldValue.delete()
    

    But if I have an object like :

    songList {
    songName1: "HelloWorld",
    songName2: "AnotherSong",
    songName3: "andTheLastOne"
    }
    

    In order to delete the field songName3, I won't be able to do something like :

    songList.songName3: firebase.firestore.FieldValue.delete()
    

    Is there a way to delete a field inside an object ? Or should I delete the whole object, rebuild it without the 3rd field and save it ?

    Thanks in advance,