Firestore更新集合中的所有文档 [英] Firestore update all documents in collections

查看:65
本文介绍了Firestore更新集合中的所有文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为用户的Firestore集合,每个用户都有一个生成的ID,其字段得分为:

I have a firestore collections named users, each users have a generated id with a field score :

users 
    0e8X3VFL56rHBxxgkYOW
        score : 4
    3SeDjrgAWMmh3ranh2u
        score : 5

我使用redux-firestore,我想将所有用户的得分重置为0,例如

I use redux-firestore and i want to reset all my users score at 0, something like

firestore.update({ collection: 'users' }, { score : 0 }

我无法实现,因为更新方法需要文档ID

I can't achieve this because update method need a document id

你知道怎么做吗?

推荐答案

您可以获取集合中的所有文档,获取其ID并使用这些ID进行更新:

You can get all the documents in the collection, get their id's and perform updates using those id's:

db.collection("cities").get().then(function(querySnapshot) {
    querySnapshot.forEach(function(doc) {
        doc.ref.update({
            capital: true
        });
    });
});

这篇关于Firestore更新集合中的所有文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆