如何在Cloud Firestore中移动文档? [英] How to move a document in Cloud Firestore?

查看:60
本文介绍了如何在Cloud Firestore中移动文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我如何在Cloud Firestore中重命名,移动或更新文档或集合名称吗?

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

反正我还能从终端或任何应用程序访问Cloud Firestore来更新我的收藏集或文档吗?

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

推荐答案

实际上,没有move方法可让您简单地将文档从一个位置移动到另一个位置.您需要创建一个.要将文档从一个位置移动到另一个位置,建议您使用以下方法:

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<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());
            }
        }
    });
}

其中fromPath是要移动的文档的位置,而toPath是要移动的文档的位置.

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.

流程如下:

  1. Get 来自 fromPath 位置的文档.
  2. Write 将文档移到 toPath 位置.
  3. Delete 来自 fromPath 位置的文档.
  1. Get the document from fromPath location.
  2. Write the document to toPath location.
  3. Delete the document from fromPath location.

就是这样!

这篇关于如何在Cloud Firestore中移动文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆