Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get all documents in a Firestore Cloud Function?

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?

like image 933
Justin Dachille Avatar asked Nov 30 '17 04:11

Justin Dachille


Video Answer


1 Answers

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);
    });
like image 121
Frank van Puffelen Avatar answered Nov 14 '22 21:11

Frank van Puffelen