[英]In Firestore, how do I change for all documents of a collection of which a certain field has a certain value, that field in a certain value?
I want to change the value of a certain field of all documents in a Cloud Firestore collection to a certain value if that field is equal to a certain value.如果某个字段等于某个值,我想将 Cloud Firestore 集合中所有文档的某个字段的值更改为某个值。 How do I do that?
我怎么做?
In addition to Doug's answer, if you want to update all documents in a collection where a field contains a certain value, then please use the following lines of code:除了道格的回答,如果你想更新一个字段包含某个值的集合中的所有文档,那么请使用以下代码行:
FirebaseFirestore db = FirebaseFirestore.getInstance();
Query query = db.collection("collName").whereEqualTo("fieldName", "fieldValue");
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
document.getReference().update("fieldToUpdate", "value");
}
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.