Damandroid
Damandroid

Reputation: 974

Cloud Functions Scheduler Function operation

I want to have some data extracted and written somewhere in Cloud Firestore so I wrote a scheduler function which is running fine up to now but it is not doing much.

For my next step I would like to understand if when this function runs, does it get ALL the documents in ALL the collections at once? Or does it iterate through the collections one by one? The reason I need to know is because I want to create some variables and reset them when the collection changes.

My current way of proceeding is this:

exports.schFun = rf.pubsub.schedule('0 6 * * *')
    .timeZone('Europe/London') // Users can choose timezone
    .onRun(async (context) => {
      // Should these be declared here?
      let totalNumAccepted = 0;
      let totalRevenueAccepted = 0.0;
      let totalNumRejected = 0;
      let totalRevenueRejected = 0.0;

      let postcode = [];
      let timeOrderPlaced = new Date();

      const querySnapshot = await db.collectionGroup("open_orders") // This is separate for each store
      .where('complete', '==', true)
      .get();
      querySnapshot.forEach((doc) => { 
      // Here onwards I want to fill the above variables with data fields in documents and reset the variables when "open_orders" collection changes...

Upvotes: 0

Views: 43

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317352

when this function runs, does it get ALL the documents in ALL the collections at once?

Yes, it does that. The snapshot will contain everything from all collections and subcollections named "open_orders".

You should not assume that the documents are grouped in any way. If you need ordering, you should explicitly order the documents by some field that they have in common.

If you want some sort of grouping based on the nesting of the subcollection, you should do that yourself by looking at each document, then figure out what group it belongs to. Perhaps you want to examine the DocumentReference ref field of each DocumentSnapshot to see where it come from, group them yourself, then iterate the groups.

Upvotes: 2

Related Questions