8

I have a scenario in which I want to move my top level, in other words first, collection to the third level. Is the only way to go about this to use third party import-export software with manipulation to do this, or is there a firestore feature that I am missing? All help will be greatly appreciated.

TopLevel>Documents>ThirdLevel, where top level is the first collection in the db. Just to give a better picture of what I am trying to say.

Hobbes
  • 107
  • 2
  • 8
  • Possible duplicate of [How to move a document in Cloud Firestore?](https://stackoverflow.com/questions/47244403/how-to-move-a-document-in-cloud-firestore) – OneHoopyFrood Oct 10 '19 at 21:12

3 Answers3

10

Firestore has no ability to move collections or documents. Instead, you can copy documents. You'll have to write code to read the contents of a document, then write those contents to a document in a new location, then delete the original document.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 24
    A feature that Google has to consider providing. – Ron FND Dec 20 '18 at 14:00
  • 5
    That along with renaming fields – ThinkDigital Jan 31 '19 at 02:47
  • 1
    Google should provide this feature just like they have for realtime database. Read, Write to new location and delete can be very costly for some cases. Move collection is very intuitive and should be provided even via console to start with – A Paracha May 03 '19 at 10:15
5

I know it's an old question, but just in case someone else needs this. It's now possible using google's cloud shell. Details available in this link: https://firebase.google.com/docs/firestore/manage-data/export-import

You have two options to export specific collections, modify them, then import them, or export the whole bucket, then import it back. As I understood you can't mix the operations for example export all and import specific collections.

Note: I believe you'll be charged for the read and write of all documents exported and imported. And billing must be activated.

aldobaie
  • 1,387
  • 10
  • 15
  • Pretty sure you can't move a collection, like the OP asked. So if you have "/users" and want to move it "/users2", that is still not possible. – DarkNeuron Jan 09 '20 at 15:17
  • @DarkNeuron Actually it’s possible. However, it’s not a one button click! I have went through the process and it worked just fine. You can download the whole database or part of it and modify it as you wish then upload it. – aldobaie Jan 09 '20 at 15:25
  • You need to elaborate on that then :-) – DarkNeuron Jan 10 '20 at 11:16
  • looks like the import has to put the collection in the same place in the tree as it was exported; so I can't move say a top level 'users' collections to a sub collection under orgs\myorg\users. If @aldobaie you have figured out how to do; fill us in. – toddwseattle Jan 23 '20 at 07:08
  • @toddwseattle, correct! If you export a collection, you have to import it in the same place for the edits to apply. I have tried exporting and importing the whole collection, and it works fine. If you want to move a collection, you should export a collection that holds both the previous and new location of that collection. – aldobaie Jan 24 '20 at 13:05
  • @aldobaie any way you could post some code for backing up /users and restoring it as /users2? Would be a huge help – user1114 Jan 26 '20 at 02:53
  • 4
    I will provide a full example in a week or so as I'm running late on my work. Give me 7-10 days and I'll post it. – aldobaie Jan 26 '20 at 10:34
  • 1
    @aldobaie :''''( – Albert Renshaw May 19 '20 at 19:42
  • @aldobaie Did you provide a full exemple somewhere ? i am very interested :) – LCMa Mar 24 '21 at 10:10
0

maybe this can help

public void moveFirestoreDocument(DocumentReference fromPath, final DocumentReference toPath) {
    fromPath.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                DocumentSnapshot document = task.getResult();
                if (document != null) {
                    toPath.set(document.getData())
                        .addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void aVoid) {
                                Log.d(TAG, "DocumentSnapshot successfully written!");
                                fromPath.delete()
                                .addOnSuccessListener(new OnSuccessListener<Void>() {
                                        @Override
                                        public void onSuccess(Void aVoid) {
                                            Log.d(TAG, "DocumentSnapshot successfully deleted!");
                                        }
                                })
                                .addOnFailureListener(new OnFailureListener() {
                                        @Override
                                        public void onFailure(@NonNull Exception e) {
                                            Log.w(TAG, "Error deleting document", e);
                                        }
                                });
                            }
                        })
                        .addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Log.w(TAG, "Error writing document", e);
                            }
                        });
                } else {
                    Log.d(TAG, "No such document");
                }
            } else {
                Log.d(TAG, "get failed with ", task.getException());
            }
        }
    });
}
Hari Agus W
  • 71
  • 1
  • 3