Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore - Delete a field inside an object

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,

like image 548
Gio Avatar asked Jan 08 '18 07:01

Gio


People also ask

How do I delete a nested field on firestore?

For v9 and newer, you use deleteField() (as in your case) For react-native-firebase , you currently use firestore. FieldValue. delete() (based on the docs, as pointed out by @AlekssandarNikolic in their answer).


5 Answers

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

like image 185
Cristiano Avatar answered Oct 17 '22 20:10

Cristiano


This work for me in general

firebase
  .firestore()
  .collection('collection-name')
  .doc('doc-id')
  .set({ songlist : {
    [songName]: firebase.firestore.FieldValue.delete()
  }
  }, { merge: true });
like image 45
Manuel Borja Avatar answered Oct 17 '22 19:10

Manuel Borja


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;
    }
}
like image 26
Paul Avatar answered Oct 17 '22 19:10

Paul


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.

like image 40
N Kurt Avatar answered Oct 17 '22 21:10

N Kurt


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!

like image 2
Mark Fonte Avatar answered Oct 17 '22 19:10

Mark Fonte