I'm trying to update/delete a fields in a Firestore document, but the fields that have a "period" in the name seem to be silently failing when trying to update/delete them. The reason I have periods is that I'm using URLs as the keys in the object, I feel this is a semi-common use case.
Example:
First create the document (this works fine)
db.collection("data").doc("temp").set({
helloworld: {
key1: 'foo'
},
hello.world: {
key1: 'bar'
}
})
If you try to delete the element without the period, it works fine.
db.collection("data").doc("temp").update({
helloworld: firebase.firestore.FieldValue.delete()
})
// Value is Deleted
If you try to delete the element without the period, it doesn't do anything.
db.collection("data").doc("temp").update({
hello.world: firebase.firestore.FieldValue.delete()
})
// Nothing Happens!
I've also tried
let u = {}
u['hello.world'] = firebase.firestore.FieldValue.delete()
db.collection("data").doc("temp").update(u)
// Nothing Happens!
Is this a bug? Are periods in field names supported? It seems strange I can create the element but not delete it.
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.
The update operation is reading
hello.world
as a dot-separated path to a field calledword
that is nested like this: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.FieldPathSo this is what you want:
You need to wrap it in quotes when you use periods in the name on an update or delete like:
or for dynamic names: