有人可以帮我解决如何在Cloud Firestore中重命名,移动或更新文档或集合名称的问题吗?
无论如何我还可以访问我的Cloud Firestore来更新终端或任何应用程序的集合或文档吗?
答案 0 :(得分:7)
实际上没有move
方法可以让您简单地将文档从一个位置移动到另一个位置。你需要创建一个。为了将文档从一个位置移动到另一个位置,我使用以下方法来吸引您:
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
是您要移动文档的位置。
流程如下:
Get
来自fromPath
位置的文档。Write
将文档转到toPath
位置。- 醇>
Delete
来自fromPath
位置的文档。
就是这样!
答案 1 :(得分:1)
以下是使用新名称获取集合的另一种变体,它包括:
$(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);
});
}
}
=====
以上是在新ID下添加项目的上一个答案
toRef
.add(doc.data())
.then(function (docRef) {
console.log('Document written with ID: ', docRef.id);
})
.catch(function (error) {
console.error('Error adding document: ', error);
});