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?
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); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With