Jakob
Jakob

Reputation: 4854

adding a calculated (count) field to firestore

I'm building an app to keep track of locations and checkins. Users check in during the day to different locations, and I want to keep a realtime overview over the number of users in each location.

I save check-in's in one collection and the locations in another.

I want to add a field to the location object that is calculated based on how many users are currently checked in to the location. Is there a way to do this on the db side, or would I have to create a new query for each of the locations?

Upvotes: 2

Views: 1652

Answers (1)

J. Doe
J. Doe

Reputation: 13033

Use Cloud Functions and transactions.

  1. Install Cloud Functions and connect with your project
  2. Add a onCreate trigger when someone checks in. This method should be called whenever a person adds data to, for example, your location (user checks in)
  3. Increase the counter with the transaction

Upvotes: 2

Related Questions