How to move a document in Cloud Firestore?

前端 未结 2 1205
迷失自我
迷失自我 2020-11-28 14:04

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 updat

2条回答
  •  再見小時候
    2020-11-28 14:32

    Actually there is no move method that allows you to simply move a document from a location to another. You need to create one. For moving a document from a location to another, I suggest you use the following method:

    public void moveFirestoreDocument(DocumentReference fromPath, final DocumentReference toPath) {
        fromPath.get().addOnCompleteListener(new OnCompleteListener() {
            @Override
            public void onComplete(@NonNull Task task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document != null) {
                        toPath.set(document.getData())
                            .addOnSuccessListener(new OnSuccessListener() {
                                @Override
                                public void onSuccess(Void aVoid) {
                                    Log.d(TAG, "DocumentSnapshot successfully written!");
                                    fromPath.delete()
                                    .addOnSuccessListener(new OnSuccessListener() {
                                            @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());
                }
            }
        });
    }
    

    In which fromPath is the location of the document that you want to be moved and toPath is the location in which you want to move the document.

    The flow is as follows:

    1. Get the document from fromPath location.
    2. Write the document to toPath location.
    3. Delete the document from fromPath location.

    That's it!

提交回复
热议问题