Home > other >  Delete a field from Firebase Firestore where the field/key has a period/punctuation (".")
Delete a field from Firebase Firestore where the field/key has a period/punctuation (".")

Time:05-06

This question is for a React Native Expo mobile App if that matters.

I have a flat database object. Using the documentation as an example:

I have added a field/key called "example.com" with some arbitrary value. The object then looks like this:

{
capital: "words",
location: "other words",
"example.com": "more words"
}

I want to delete the key "example.com" key from the database, and I am using pretty much the same code as in the example from the documentation.

const cityRef = doc(db, "cities", "BJ");
const payload = {
    "example.com": deleteField(),
};
await updateDoc(cityRef, payload);

This does not delete the "example.com" key. I understand that this attempts to delete the "com" key from the "example" object within the main database document, which is not what I want, but I do not understand how to rework this code to treat the entire string as a key.

I have looked at some similar questions here on SO(links at the bottom) that came to the conclusion that they had to use FieldPath, but the syntax is outdated, and no longer works on the current version of Firestore(or at least I can not find the documentation for FieldPath for the version I am using, linked above).

And tips on how to get this working (deleting a field with a period) would be appreciated.

Link to similar problems:

CodePudding user response:

You still need to use FieldPath, but the API is different in the new v9 module SDK. You can see FieldPath in the API docs. Just import it along with any other functions that you use from the Firestore SDK.

I haven't used it myself, but based on the linked docs, my guess is that it works like this:

import { updateDoc, deleteField, FieldPath } from "firebase/firestore";
...
updateDoc(cityRef, new FieldPath("example.com"), deleteField());
  • Related