是否可以在Firestore Cloud Function中获取所有文档? [英] Is it possible to get all documents in a Firestore Cloud Function?
问题描述
我想使用云函数更新集合中所有文档的值,因为它们取决于创建日期,但是通过Cloud Firestore触发器示例来看,似乎所有事件只能访问单个文档.有没有办法遍历所有文档?
I want to update a value on all documents in a collection using cloud functions as they are dependent on the creation date, but looking through the Cloud Firestore Triggers examples it looks like all events are only able to access a single document. Is there a way to loop over all documents?
推荐答案
您通常会在Cloud Functions代码中使用Admin SDK.一旦有了它,就可以照常阅读该集合了.在从集合中读取所有文档时,请查看 firebase文档中的节点示例. :
You'd typically use the Admin SDK for that in your Cloud Functions code. Once you have that, it's a matter of read the collection as usual. Check the node examples in the firebase documentation on reading all documents from a collection:
const admin = require('firebase-admin');
admin.initializeApp({
credential: admin.credential.applicationDefault()
});
var db = admin.firestore();
var citiesRef = db.collection('cities');
var allCities = citiesRef.get()
.then(snapshot => {
snapshot.forEach(doc => {
console.log(doc.id, '=>', doc.data());
});
})
.catch(err => {
console.log('Error getting documents', err);
});
这篇关于是否可以在Firestore Cloud Function中获取所有文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!