Updating a collection name in Cloud Firestore

16,698

Solution 1

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.

Solution 2

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.

Solution 3

You can try Firefoo it allows you to change the rename, duplicate, export etc, but it is not free enter image description here

Solution 4

Here is a piece of code written in JavaScript. Please consider that subcollections are not moved, and that the former collection is not destroyed.

const db = admin.firestore();
const oldCollRef = db.collection('oldCollectionName');
const oldCollSnap = await oldCollRef.get();
let arrayPromise = [];
oldCollSnap.forEach(async doc => {
    arrayPromise.push(new Promise(async (resolve, reject) => {
        resolve(await db.collection('newCollectionName').add(doc.data()));
    }));
})
Promise.all(arrayPromise)
Share:
16,698
Sri
Author by

Sri

Updated on June 29, 2022

Comments

  • Sri
    Sri almost 2 years

    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.

  • Sri
    Sri almost 6 years
    There is option to export/import in Firebase real-time database but not for Firestore. Please correct me in case I have missed it somewhere. Is there any other option to export documents in Firestore?
  • AdamHurwitz
    AdamHurwitz over 5 years
    @DougStevenson, from what I understand it is only possible to import a specific collection, not documents, thus retaining the collection name. Is it possible to import all of the documents under a collection into another collection in the Firestore Db that has a different collection name?
  • Oleg Novosad
    Oleg Novosad over 4 years
    @Sri you can export / import full Firestore database into GCP bucket using Firebase import / export and then update the exports manually (or by script) and import back as updated.
  • dtheodor
    dtheodor over 3 years
    Don't put changing IDs in the same bucket with changing collection names. Changing a collection name should have nothing to do with indexing.
  • Sam Pierce Lolla
    Sam Pierce Lolla over 3 years
    This would not move over any sub collections though, right? Just flagging for people in that situation.
  • Shalabyer
    Shalabyer over 3 years
    Haven't tested it with the existence of subcollection, but I think it will work as the subcollection is considered to be within the data that is mentioned in 'element.data()' right? so why wouldn't it be moved? if someone can test it I would be pleased.
  • skillit zimberg
    skillit zimberg almost 3 years
    I used @JeremyValensi's solution above to call Andrey Lechev's solution for each doc to get the sub-collections. Thank you so much to both!
  • Godwin
    Godwin almost 3 years
    I tried it but it keeps showing this error "The argument type 'Object?' can't be assigned to the parameter type 'Map<String, dynamic>" when i ty to SET to it
  • Shalabyer
    Shalabyer about 2 years
    @Godwin answer updated