[英]How to update document in firestore using collection reference?
I'm having a problem in updating my document using collection reference in firestore android. 我在使用Firestore Android中的集合引用更新文档时遇到问题。 It is auto-generated id so I don't even know what is the id of my document. 它是自动生成的ID,所以我什至不知道我的文档ID是什么。
Save the id when retrieving the data eg 检索数据时保存ID,例如
db.collection("cities")
.whereEqualTo("capital", true)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
City city = document.toObject(City.class);
city.setId(document.getId()); //This is what you are looking for
...
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
You can that when updating 您可以在更新时
city.setName("LA"); //edit object
db.collection("cities").document(city.id).set(city); //save object
PS I have not run this code - could have syntax errors, careful not to copy paste. PS我没有运行此代码-可能有语法错误,请注意不要复制粘贴。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.