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,
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).
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
This work for me in general
firebase
.firestore()
.collection('collection-name')
.doc('doc-id')
.set({ songlist : {
[songName]: firebase.firestore.FieldValue.delete()
}
}, { merge: true });
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;
}
}
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With