简体   繁体   English

如何在 Firestore 中获取文档中的集合数量?

[英]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.所以我正在做一个 android 应用程序,我需要获取数据库中的用户数量来为下一个将注册的用户设置 ID。 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.所以“app”是一个集合,“users”是一个文档,里面是代表每个用户的集合。 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. Firestore不能那样工作,您可以使用 SQL 数据库执行此操作,Firestore 由 google 云数据存储支持,该数据存储将数据作为键值对保存,您不能依赖它来生成唯一 id ......如果您需要要获取数据库中的用户数,那么您应该创建另一个数据来保存该计数,并且您应该在添加新数据时增加计数并在删除数据时减少计数。 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.另一方面,如果您想唯一标识数据库中的每个项目,则可以使用从数据库生成的密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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