Cannot update/delete Firestore field with "period" in the name
The update operation is reading hello.world
as a dot-separated path to a field called word
that is nested like this:
{
hello: {
world: "Some value"
}
}
If you have a field with a dot in the name you need to use FieldPath
to refer to it literally in an update:
https://firebase.google.com/docs/reference/js/firebase.firestore.FieldPath
So this is what you want:
doc.update(
firebase.firestore.FieldPath("hello.world"),
firebase.firestore.FieldValue.delete());
None of the other answers worked for me, the closest one had a new()
keyword missing, here is what worked for me
let fpath = new firestore.firestore.FieldPath(`hello.${world}`);
doc.update(
fpath,
firestore.firestore.FieldValue.delete()
);
I found a workaround if you are using dynamic keys and J Livengood's solution doesn't work for you. You can use the "set" method with "merge: true" to selectively set the key with the delete value.
var dynamicKey = "hello.world"
// ES6
db.collection("data").doc("temp").set({
[dynamicKey]: firebase.firestore.FieldValue.delete()
}, { merge: true })
// ES5
var obj = {}
obj[dynamicKey] = firebase.firestore.FieldValue.delete()
db.collection("data").doc("temp").set(obj, { merge: true })
You need to wrap it in quotes when you use periods in the name on an update or delete like:
db.collection("data").doc("temp").update({
"hello.world": firebase.firestore.FieldValue.delete()
})
or for dynamic names:
[`hello.${world}`]: firebase.firestore.FieldValue.delete()