Firestore 更新集合中的所有文档 [英] Firestore update all documents in collections
本文介绍了Firestore 更新集合中的所有文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为 users 的 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屋!
查看全文