Firestore Data Aggregation
Last updated
Last updated
The Firestore Data Aggregation allows you calculate averages, sums or count documents at query level, which can be useful to aggregate information without the need to "read" all documents, saving document reads and reducing the amount of data broght to the front-end.
Here you must specify the id (or name) of the collection you want to query data from. You can subcollections paths to choose a subcollection (e.g. collection/doc_id/subcollection).
The plugin will query data from the Firestore, running front-end process on Bubble. That way, no Workload units will be consumed. 🙂
Count Performs a document count query from Firestore, respecting the query filters you defined. Obs.: Each 1000 blocks of document accounted for will be billed as 1 document read on Firestore (e.g. 1500 documents counted will be billed for 2 reads)
Average Calculates the average value of a field you specify for the list of documents being queried. If the field is not quantifiable, the query will result in a error.
Sum Calculates the sum of a field you specify for the list of documents being queried. If the field is not quantifiable, the query will result in a error.
The field of the list of documents you with to aggregate.
Will be ignored for "count" aggregations.
You can choose a field to order your list of document from, at query level, specifying the sort direction (descending = yes | ascending = no).
This options allow you to choose fields from which to filter your query.
You can choose to ignore empty constraints, which will simple "skip" the filter if the input you provided is empty.
<
Gets values less than the provided input (must be of type number or date)
<=
Gets values less than or equal to the provided input (must be of type number or date)
==
Gets values equal to the provided input.
>
Gets values greater than the provided input (must be of type number or date).
>=
Gets values greater than or equal to the provided input (must be of type number or date).
array-contains
Gets values where the field of type array/list has the value provided as input.
array-contains-any
Gets values where the field of type array/list any of the array of values provided as input.
in
Gets values where the field value is present on the array of values provided as input.
not-in
Gets values where the field value is not present on the array of values provided as input.
Firestore supports only one filter of type "array-contains", "array-contains-any", "in" or "not-in" per query. If you try to use more than one, the first one will be considered by the plugin, but the other's will be ignored.
Since Bubble is not flexible on the type of data we can define on plugins, the best way to implement our plugin was to work with texts. So each value you put on query values must be interpreted the right way to perform the Firestore Query. Defining a "query filter value type" can help us get it right.
So here are the types of query filter values:
auto
The plugin will try to infer the type from the input you provided.
text
Treated as a text value.
number
Treated as a number value.
boolean (true/false)
Treated as a true or false (yes/no) value.
date
Treated as a Date type. The plugin will convert the value to a Date.
array
Treated as a list in JavaScript format (e.g. ["item1", "item2", "item3"]). If you need to use it on Bubble's lists, you can apply the ":format as text" message to the list, to build the array string into that format.
reference (coll/doc_id)
Treated as a document reference. Document references are always handled by the plugin as paths to a document. For instance, if you want to get the reference for a document from the "tasks" collection with an id "1234", you should right "tasks/1234"
The result of the aggregation query, always a number, that can be a count, average or sum.
The name of the collection from which the element is getting it's data from.
If the query fails and returns an error, the error message will be stored on this state.
This action reloads the elements data, performing the query again. This might be useful to provide consistency in your data, since the aggregation query can't be dynamic.
This event is triggered when the query data is ready.
Keep in mind that, bacause the query operations run on the front-end, Bubble sometimes messes with the order in which data arrives from Firestore. That means you must account that factor while using results of other Firebase elements as input data in other query elements.
For instance, if you want to get documents form the "tasks" collection where the "owner" filter equals the current user's document reference, you can use the filter bellow (owner == users/Firebase Auth's User ID).
But if the User ID is not yet available, the query will fail due to security rules on Firestore.
To work around that, you can put a visibility conditional on the Firestore Data element, for it to show only when the User ID is not empty. As the query will only occur when the element is "visible" on the page, it will wait for when the User ID is available to run the query, avoiding the error.
Keep that in mind when fetching data from Firestore.
This can affect the result, as sorting can behave as a filter in queries if the sorting field is not present in a document. You should use this setting to reproduce the exact settings of a element you want to count document from, for instance.
You can read more on the .
Firestore does not offer "text" filters like the Bubble "contains keywords" option, because of the way they structure the database using indexes. For this kind os searches they recomend the use of external services like .