Home > Mobile >  Google Cloud Functions Firestore Limitations
Google Cloud Functions Firestore Limitations

Time:02-24

I have written a function which gets a Querysnapshot within all changed Documents of the past 24 hours in Firestore. I loop through this Querysnapshot to get the relevant informations. The informations out of this docs I want to save into maps which are unique for every user. Every user generates in average 10 documents a day. So every map gets written 10 times in average. Now I'm wondering if the whole thing is scalable or will hit the 500 writes per transaction limit given in Firebase as more users will use the app.

The limitation im speaking about is documented in Google documentation.

Furthermore Im pretty sure that my code is really slow. So im thankful for every optimization.

exports.setAnalyseData = functions.pubsub
  .schedule('every 24 hours')
  .onRun(async (context) => {

    const date = new Date().toISOString();
    const convertedDate = date.split('T');

    //Get documents (that could be way more than 500)
    const querySnapshot = await admin.firestore().collectionGroup('exercises').where('lastModified', '>=', `${convertedDate}`).get();
    //iterate through documents
    querySnapshot.forEach(async (doc) => {

      //some calculations

      //get document to store the calculated data 
      const oldRefPath = doc.ref.path.split('/trainings/');
      const newRefPath = `${oldRefPath[0]}/exercises/`;
      const document = await getDocumentSnapshotToSave(newRefPath, doc.data().exercise);

      document.forEach(async (doc) => {
        //check if value exists
        const getDocument = await admin.firestore().doc(`${doc.ref.path}`).collection('AnalyseData').doc(`${year}`).get();

        if (getDocument && getDocument.exists) {

          await document.update({
            //map filled with data which gets added to the exisiting map
          })
        } else {
          await document.set({
            //set document if it is not existing
          }, {
            merge: true
          });
          await document.update({
            //update document after set
          })
        }
      })
    })
  })

CodePudding user response:

The code you have in your question does not use a transaction on Firestore, so is not tied to the limit you quote/link.

I'd still recommend putting a limit on your query through, and processing the documents in reasonable batches (a couple of hundred being reasonable) so that you don't put an unpredictable memory load on your code.

  • Related