如何在Cloud Firestore中移动文档? [英] How to move a document in 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.
流程如下:
-
Get
来自fromPath
位置的文档. -
Write
将文档移到toPath
位置. -
Delete
来自fromPath
位置的文档.
Get
the document fromfromPath
location.Write
the document totoPath
location.Delete
the document fromfromPath
location.
就是这样!
这篇关于如何在Cloud Firestore中移动文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!