Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating a collection name in Cloud Firestore

Can we change the name of a collection in Cloud Firestore? I have created a collection with 200 documents. Now I want to change the name of the collection. In the Firebase console, I could not find a way to do this.

Is it possible to change a collection's name either through code or in the console. Or Is it possible to export documents from Firestore, create a new collection and then import those documents again?

Please help.

like image 753
Sri Avatar asked Sep 10 '18 05:09

Sri


People also ask

How do I change the name of a firestore collection?

Right-click the collection in the sidebar and select Rename Collection.

How do I update Subcollection in firestore?

You just need to get a reference to the subcollection, which you get from the DocumentReference . And on that you simply use the regular method for writing data again: firebase.google.com/docs/firestore/manage-data/add-data. If you're having a hard time making this work, update your question to show what you've tried.


2 Answers

It's not possible to change the names or IDs or collections or documents once they exist. All that data is immutable, for the purpose of building efficient indexes.

You could certainly read everything out, then put it all back in with new names and IDs.

like image 98
Doug Stevenson Avatar answered Oct 02 '22 09:10

Doug Stevenson


You can load it then reupload it with the new name

      await FirebaseFirestore.instance           .collection(oldCollectionName)           .get()           .then((QuerySnapshot snapShot) async {         snapShot.docs.forEach((element) async {           await FirebaseFirestore.instance               .collection(newCollectionName)               .doc(element.id)               .set(element.data());         });       }); 

Note: the above code is written for flutter, but it might help anyone with little changes.

like image 34
Shalabyer Avatar answered Oct 02 '22 09:10

Shalabyer