Luís Fernandes
Luís Fernandes

Reputation: 21

How can I get the number of collections inside a document in Firestore?

So I'm doind an android app and I need to get the number of users in the database to set the id for the next user that will register. My data model looks like this:

app:
   users:
        1:
         email:"[email protected]"
         isTherapist: true
        2:
         email:"[email protected]"
         isTherapist: false

So "app" is a collection, "users" is a document and inside it are collections tha represent each user. How can I get this done?

Upvotes: 1

Views: 49

Answers (1)

oziomajnr
oziomajnr

Reputation: 1741

Firestore doesn't work like that, you can do this with an SQL database, Firestore is backed by google cloud data store that holds the data as key-value pairs, you cannot rely on it to generate unique ids... If you need to get the number of users in your database then you should create another Data to hold that count and you should increment the count when new data is added and decrement the count when data is deleted. If you expect the data to be updated frequently you can use multiple counters and pick anyone to increment randomly.

On the other hand, if you want to uniquely identify each item in your database then you could use the key that is generated from the database.

Upvotes: 1

Related Questions