TobiasBuchholz / Plugin.Firebase

Wrapper around the native Android and iOS Firebase Xamarin SDKs
MIT License
220 stars 49 forks source link

[Question] [Firestore] How to Count documents with aggregation queries #190

Closed vhugogarcia closed 1 year ago

vhugogarcia commented 1 year ago

Hello @TobiasBuchholz,

I hope you are doing well!

I would like to know if it is possible to perform on Firestore an aggregation query like the Count() described on the Firebase documentation: https://firebase.google.com/docs/firestore/query-data/aggregation-queries

I did a research, however, I could not find on the official documentation how to perform an aggregation query: CloudFirestore Getting Started

For aggregation queries such as count(), you are charged one document read for each batch of up to 1000 index entries matched by the query. This is very helpful in an effort to minimize the costs when trying to fetch the data to get totals in Firestore.

I went ahead and tried this approach on my app, however, I'm afraid that it will not be an aggregation query and instead it will perform a read operation first and for instance increase the usage on READ for the Firestore quota:

public static async Task<int> Count(Location location)
    {
        string path = $"/recipes/demo/ingredients/12345678";
        var collectionReference = CrossFirebaseFirestore.Current.GetCollection(path);
        var snapshot = await collectionReference.GetDocumentsAsync<Ingredient>();

        if (snapshot is not null)
        {
            return snapshot.Count;
        }

        return -1;
    }

Based on the official Google's documentation I found that the Count is obtained on the CollectionReference: const snapshot = await collectionRef.count().get(); however, I tried to find the Count on the collection reference without luck.

So my question is, do you know if the snapshot.Count is going to work as an aggregation query in Firestore?

Thanks in advance!

TobiasBuchholz commented 1 year ago

Hi @vhugogarcia, thanks for the interesting question! Since the aggregation queries are a fairly new feature of firestore the current version of the plugin is not supporting it yet. Unfortunately the newest version of the iOS native firestore binding library is 9 months old and doesn't contain this feature yet, so we'll have to wait until there is an update. The newest version of the android binding library, which was released a few weeks ago, does contain this feature, so there is hope the update for iOS will come soon. When that's the case I will update the plugin accordingly :)

vhugogarcia commented 1 year ago

Thanks @TobiasBuchholz for the helpful response.

Well, let's hope the iOS native firestore binding library gets updated to latest Firebase SDK versions. 🤞🏻 I went ahead and asked some friends on Discord if they have still that planned. I noticed the iOS binding is pointing to version 8 still, and currently Firebase SDK is on version 10.

Looking forward for this in the future. Thanks again @TobiasBuchholz 😃