简体   繁体   中英

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:"dummy@gmail.com"
         isTherapist: true
        2:
         email:"dummy@gmail.com"
         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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM