AggregateField is not defined

I'm new to Retool, and I'm trying to perform a basic Aggregate Sum function on my Firestore Collection as follows:

const gigsRef = db.firestore().collection("gigs");
const sumAllShiftsPosted = gigsRef.aggregate({
  totalShiftsPosted: AggregateField.sum("numSpotters"),
});
const snapshot =  await sumAllShiftsPosted.get();
return snapshot.data().totalShiftsPosted;

I'm getting the following error:
'AggregageField' is not defined

What am I missing? Does Retool not support aggregate functions? If so, how do I achieve the result I expect above?

Hi @ikartik90, I don't believe Firestore supports aggregation queries natively in the way you're trying to use it in the code pasted above- I don't see the aggregate method or an AggregateField class in it's JavaScript SDK which is likely why you're seeing that error. However, you should be able to achieve your goal of summing up 'numSpotters' in you 'gigs' collection by creating a javascript transformer that loops through the documents and sums up the numSpotters. Something like this should work:

let data = {{firestoreQuery.data}}; 
let totalShiftsPosted = 0; 
for (let i = 0; i < data.length; i++) { 
   totalShiftsPosted += data[i].numSpotters; 
} 
return totalShiftsPosted;

Your are using an outdated version of firebase-admin. AggregationField is introduced in version 12 so run npm i firebase-admin@12.1.0 in your functions directory

Lindsay, Firestore admin 've already support aggregation queries natively.
You need to update firebase-admin of your Retool js SDK to 12.x.x to support it.
I can do aggregation in my cloud function, but cannot do it with Retool.
https://firebase.google.com/docs/firestore/query-data/aggregation-queries#node.js_2

1 Like

Is this supported now?

https://firebase.google.com/docs/firestore/query-data/aggregation-queries

There are clear speed advantages to using them over summing up on the client

I think you guys only need to update the firebase sdk?

1 Like

This should have been added in day - 1. Aggregation is a big part of NoSQL databases and a firebase integration without the ability to do any type of aggregation is not really an integration.

We are making a function call JUST to do aggregation queries, which should't be the case.

Thanks everyone for raising this issue. We have an open request to make this upgrade, and it is looking likely however, this most likely won't be prioritized by the end of this year. I will follow up here as soon as any update have rolled out! We appreciate your feedback and understand the frustration.