简体   繁体   English

在 Firestore 中,如何更改某个字段具有某个值的集合的所有文档,该字段具有某个值?

[英]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?我怎么做?

  1. Query with a filter that matches all of the documents you want to chage.使用与您要查询的所有文档匹配的过滤器进行查询。
  2. Iterate the results of that query.迭代该查询的结果。
  3. Update each document with the new field value it should contain.使用它应该包含的新字段值更新每个文档。

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.

相关问题 检查对象列表中的字段是否具有特定值? - Checking that a field in a List of Objects has a certain value? Hamcrest:测试列表包含一个具有特定值的私有字段的项目 - Hamcrest: test list contains an item that has a private field with a certain value 如何在Playframework中更新模型的特定字段? - How do I update a certain field of a Model in Playframework? 如何获取 Firestore 集合中所有文档的名称(一个字段)? - How to fetch the names (a field) of all the documents in a Firestore collection? 无论如何在给定字段名称的情况下获取对象中某个字段的值? - Is there anyway to get the value of a certain field in an object given the field name? 如何验证Java中某个位置的大量整数是否是某个值? - How do I verify that an integer value within a large number in a certain position is a certain value in Java? 如何检查数组中的所有值是否都具有特定值? - How can I check if all values in an array have a certain value? 断言 Optional 有一定的价值 - Assert that Optional has certain value 检查集合是否包含与某个特定值匹配的所有值 - Check a collection if it contains all the values that match a certain value 我如何在某个子字符串值之后收集所有内容 - how do i collect everything after a certain substring value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM