Firestore Data Aggregation

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.


Settings

Firestore Collection

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. 🙂

Aggregation Type

  • 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.

Field

The field of the list of documents you with to aggregate.

Query sort / order by options (list)

You can choose a field to order your list of document from, at query level, specifying the sort direction (descending = yes | ascending = no).

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 Firestore Data element you want to count document from, for instance.

Query filters (list) (count)

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.

Available query operators

You can read more on the Cloud Firestore documentation.

Query filter operators
Operator
Description

<

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.

Types of filter values

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:

Query filter value types
Type
How it will be handled

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"

Arrays and lists are always handled in JavaScript notation, which means each value should be comma separated and must be between brackets.

An array of number should be provided as a text like: [1, 3, 45, 2]

An array of texts should contain balues between quotes: ["text1", "text2", "text3"]

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"


States and Values

Result

The result of the aggregation query, always a number, that can be a count, average or sum.

Collection

The name of the collection from which the element is getting it's data from.

Error message

If the query fails and returns an error, the error message will be stored on this state.


Element Actions

Refresh (list)

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.


Events

Data ready or changed

This event is triggered when the query data is ready.


Using query results as inputs in other queries

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.

Last updated