I'm currently working on an app where I have to retrieve data from Google's Firestore.
My data structure looks like this:
users - [email protected] - more data
Is there a way for me to change the [email protected] to just name? I can't see a method to change the name of a document.
There is no option to rename/move a document in Cloud Firestore. You will have to copy the data to the new document and then remove the original document. See stackoverflow.com/questions/47244403/…
There are several ways to write data to Cloud Firestore: Set the data of a document within a collection, explicitly specifying a document identifier. Add a new document to a collection. In this case, Cloud Firestore automatically generates the document identifier.
You can’t change the name of a collection. It’s immutable, and it must be that way in order for indexing to be efficient. Your only real option is to create a new collection and copy all the documents into it, then delete all the original documents. Is Firebase a database? If so, what kind of information does it store? Not exactly.
Data types. Cloud Firestore lets you write a variety of data types inside a document, including strings, booleans, numbers, dates, null, and nested arrays and objects. Cloud Firestore always stores numbers as doubles, regardless of what type of number you use in your code.
I think the best way to do this is to get the data from '[email protected]' and upload it to a new document called 'name' and then delete the old one.
Just as an example:
const firestore = firebase.firestore(); // get the data from '[email protected]' firestore.collection("users").doc("[email protected]").get().then(function (doc) { if (doc && doc.exists) { var data = doc.data(); // saves the data to 'name' firestore.collection("users").doc("name").set(data).then({ // deletes the old document firestore.collection("users").doc("[email protected]").delete(); }); } });
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