这是我的Firestore数据库结构:
Faculty (Collection)
--Document1
-memberId: "77777"
-fullName: "Vince Melmar Ybanez"
--Document2
-memberId: "12345"
-fullName: "John Doe"
--Document3
AnnualDue (Sub-Collection of Document1 in Faculty)
--Document1
-memberId: "77777"
-fullName: "Vince Melmar Ybanez"
--Document2
-memberId: "77777"
-fullName: "Vince Melmar Ybanez"
AnnualDue (Sub-Collection of Document2 in Faculty)
--Document3
-memberId: "12345"
-fullName: "John Doe"
AnnualDue (Collection)
--Document1
-memberId: "77777"
-fullName: "Vince Melmar Ybanez"
--Document2
-memberId: "77777"
-fullName: "Vince Melmar Ybanez"
--Document3
-memberId: "12345"
-fullName: "John Doe"
如何在教员收藏、年度到期收藏和年度到期子收藏中将成员1(文件1(的fullName
从Vince Melmar Ybanez更新为Vince Ybanez?
如果您想一次性更新多个集合或子集合中存在的多个文档,请注意这是不可能的。要解决这个问题,您必须创建三个不同的操作。
现在能够更新";fullName";的";成员1";从";Vince Melmar Ybanez";至";Vince Ybanez";在教员收藏中,请使用以下代码行:
DocumentReference facultyDocOneRef = db.collection("Faculty").document("Document1");
Map<String, Object> update = new HashMap<>();
update.put("fullName", "Vince Ybanez");
facultyDocOneRef.update(update).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "Document updated successfully.");
} else {
Log.d(TAG, task.getException().getMessage()); //Never ignore potential errors!
}
}
});
现在,为了能够更新其他集合中剩下的两个字段,您只需要创建引用并使用与上面完全相同的代码:
DocumentReference annualDueDocOneRef = db.collection("AnnualDue").document("Document1");
annualDueDocOneRef.update(update).addOnCompleteListener(/*...*/);
最后一个:
DocumentReference facultyAnnualDueDocOneRef = db
.collection("Faculty").document("Document1")
.collection("AnnualDue").document("Document1");
facultyAnnualDueDocOneRef.update(update).addOnCompleteListener(/*...*/);