简体   繁体   English

如何使用 flutter 更新 Firestore 中所有文档的单个字段?

[英]How to update a single field of all documents in firestore using flutter?

I have a 100 user documents in database, and I have to update one field to 0 in all documents.我在数据库中有 100 个用户文档,我必须在所有文档中将一个字段更新为 0。 How do we do it?我们该怎么做呢?

Firestore doesn't offer a SQL-like "update where" command that updates everything at once, so you will have to: Firestore 不提供类似 SQL 的“update where”命令来一次更新所有内容,因此您必须:

  1. Query for the documents to update查询要更新的文档
  2. Iterate each DocumentSnapshot and get its DocumentReference object using the reference property迭代每个DocumentSnapshot并使用reference 属性获取其 DocumentReference object
  3. For each document, update the field with its new value对于每个文档,使用其新值更新字段

Try doing the following尝试执行以下操作

  1. Query the documents to update查询要更新的文档
  2. Go through each DocumentSnapshot and get its DocumentReference object using the reference property and for each document, update the field with its new value Go 通过每个DocumentSnapshot并使用 reference 属性获取其DocumentReference object 并且对于每个文档,使用其新值更新字段
    void updateNotificationCount() async {
        FirebaseUser _currentUser = await FirebaseAuth.instance.currentUser();
        String authid = _currentUser.uid;
        var snapshots =
            activityFeedRef.document(authid).collection('feedItems').snapshots();
        try {
          await snapshots.forEach((snapshot) async {
            List<DocumentSnapshot> documents = snapshot.documents;
    
            for (var document in documents) {
              await document.reference.updateData(<String, dynamic>{
                'seen': true,
              });
            }
          });
        } catch (e) {
          print(e.toString());
        }
      }

The code above simply updates all unread notification from false to true immediately the user clicks on the notification tray上面的代码只是在用户单击通知托盘时立即将所有未读通知从 false 更新为 true

As @Doug mentioned there's no direct way, you'll have to query the data, get the DocumentReference from QueryDocumentSnapshot and invoke update on it.正如@Doug提到的那样,没有直接的方法,您必须查询数据,从QueryDocumentSnapshot获取DocumentReference并对其调用update

var collection = FirebaseFirestore.instance.collection('collection');
var querySnapshots = await collection.get();
for (var doc in querySnapshots.docs) {
  await doc.reference.update({
    'single_field': 'newValue',
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 Cloud Firestore 中的单个文档字段保存到 flutter 中的局部变量中? - How to save a single document field in cloud firestore into a local variable in flutter? 如何使用 Flutter 删除 Firestore 字段数组的某些子项 - How to delete certain children of Firestore field array using Flutter Firestore:如何更新现有字段 - Firestore: How to update an existing field 添加新字段或更改所有 Firestore 文档的结构 - Add new field or change the structure on all Firestore documents MongoDB:更新所有文档中的字符串字段 - MongoDB: Update a string field in all documents 如何使用 Flutter 检查 Firestore 中多个文档中的特定值 - How to check a specific Value in multiple Documents in Firestore with Flutter 如何使用 Flutter 在 Firestore 中删除具有特定值的多个文档 - How to delete multiple documents with specific Value in Firestore with Flutter 如何使用pymongo遍历按整数字段排序的集合中的所有文档? - How do I iterate over all documents in a collection sorted by an integer field using pymongo? 如何在 Firestore 索引配置文件中添加单字段豁免 - How to add Single-field Exemptions in Firestore Index Config File 如何更新 flutter sqflite 中的单列? - How to update single column in flutter sqflite?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM