Firebase 使用Firestore颤振web:在每个文档中设置一个字段作为值

Firebase 使用Firestore颤振web:在每个文档中设置一个字段作为值,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,我希望在集合的每个文档中,将特定字段设置为0。我如何才能做到这一点?在dart中要做到: 获取所有文档和更新字段: QuerySnapshot snapshot = await FirebaseFirestore.instance .collection("collectionName").doc("documentName").get(); snapshot.docs.forEach((doc) { FirebaseFir

我希望在集合的每个文档中,将特定字段设置为0。我如何才能做到这一点?

在dart中要做到:

获取所有文档和更新字段:

QuerySnapshot snapshot = await FirebaseFirestore.instance
        .collection("collectionName").doc("documentName").get();

snapshot.docs.forEach((doc) { 
      FirebaseFirestore.instance.collection("collectionName").doc(doc.id).update({
      "fieldName": 0,
    });
});
但是我建议使用firebase管理员进行此类活动,因为如果有许多文档可以超出Firestore中的使用限制,请继续关注!使用firebase admin,您可以使用不同类型的语言(例如python),并在单个活动中通过批处理写入一次更新所有内容

Python Firebase管理中的示例:

reference = db.collection(u'themes')

query_snapshot = reference.where(u'collections', u'array_contains', u'reflections').stream()

batch = db.batch()

for doc in query_snapshot:
  doc_data = doc.to_dict()

  new_data = {}
  new_data["keywords_pt"] = generate_keywords(doc_data[u"title-pt"].lower())
  new_data["keywords_es"] = generate_keywords(doc_data[u"title-es"].lower())
  new_data["keywords_en"] = generate_keywords(doc_data[u"title-en"].lower())

  batch.update(reference.document(doc.id), new_data)

batch.commit()
有关更多信息:

Firebase管理员:

批处理写入: