'How to move a document in Cloud Firestore?

Can someone help me how to rename, move or update document or collection names in Cloud Firestore?

Also is there anyway that I can access my Cloud Firestore to update my collections or documents from terminal or any application?



Solution 1:[1]

Here's another variation for getting a collection under a new name, it includes:

  1. Ability to retain original ID values
  2. Option to update field names

    $(document).ready(function () {

        FirestoreAdmin.copyCollection(
            'blog_posts',
            'posts'
        );

    });

=====

var FirestoreAdmin = {

    // to copy changes back into original collection
    // 1. comment out these fields
    // 2. make the same call but flip the fromName and toName 
    previousFieldName: 'color',
    newFieldName: 'theme_id',

    copyCollection: function (fromName, toName) {

        FirestoreAdmin.getFromData(
            fromName,
            function (querySnapshot, error) {

                if (ObjectUtil.isDefined(error)) {

                    var toastMsg = 'Unexpected error while loading list: ' + StringUtil.toStr(error);
                    Toaster.top(toastMsg);
                    return;
                }

                var db = firebase.firestore();

                querySnapshot.forEach(function (doc) {

                    var docId = doc.id;
                    Logr.debug('docId: ' + docId);

                    var data = doc.data();
                    if (FirestoreAdmin.newFieldName != null) {

                        data[FirestoreAdmin.newFieldName] = data[FirestoreAdmin.previousFieldName];
                        delete data[FirestoreAdmin.previousFieldName];
                    }

                    Logr.debug('data: ' + StringUtil.toStr(data));

                    FirestoreAdmin.writeToData(toName, docId, data)
                });
            }
        );
    },

    getFromData: function (fromName, onFromDataReadyFunc) {

        var db = firebase.firestore();

        var fromRef = db.collection(fromName);
        fromRef
            .get()
            .then(function (querySnapshot) {

                onFromDataReadyFunc(querySnapshot);
            })
            .catch(function (error) {

                onFromDataReadyFunc(null, error);
                console.log('Error getting documents: ', error);
            });
    },

    writeToData: function (toName, docId, data) {

        var db = firebase.firestore();
        var toRef = db.collection(toName);

        toRef
            .doc(docId)
            .set(data)
            .then(function () {
                console.log('Document set success');
            })
            .catch(function (error) {
                console.error('Error adding document: ', error);
            });

    }

}

=====

Here's the previous answer where the items are added under new IDs

        toRef
            .add(doc.data())
            .then(function (docRef) {
                console.log('Document written with ID: ', docRef.id);
            })
            .catch(function (error) {
                console.error('Error adding document: ', error);
            });   

Solution 2:[2]

I solved this issue in Swift. You retrieve info from the document, put it into a new document in new location, delete old document. Code looks like this:

    let sourceColRef = self.db.collection("/Users/users/Collection/")
    colRef.document("oldDocument").addSnapshotListener { (documentSnapshot, error) in
                if let error = error {
                            print(error)
                        } else {
                            DispatchQueue.main.async {
                            if let document = documentSnapshot?.data() {
                                
                                let field1 = document["Field 1"] as? String // or Int, or whatever you have)
                                let field2 = document["Field 2"] as? String
                                let field3 = document["Field 3"] as? String
                                
                                
                  let newDocRef = db.document("/Users/users/NewCollection/newDocument")
                                newDocRef.setData([
                                        "Field 1" : field1!,
                                        "Field 1" : field2!,
                                        "Field 1" : field3!,
                                        
                                ])
                            }
                          }
                        }
                    }
            
            sourceColRef.document("oldDocument").delete()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Kirill Yakovlev