Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change the name of a document in Firestore?

Tags:

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.

like image 318
Mathias Schrooten Avatar asked Dec 19 '17 11:12

Mathias Schrooten


People also ask

How do I rename a document in Cloud Firestore?

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/…

How do I write data to Cloud Firestore?

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.

How do I change the name of a collection in Firebase?

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.

What types of data can be stored in Firestore?

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.


1 Answers

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();         });     } }); 
like image 196
Bjørn Reemer Avatar answered Sep 22 '22 14:09

Bjørn Reemer