Cloud Firestore collection count

后端 未结 17 1334
庸人自扰
庸人自扰 2020-11-22 09:25

Is it possible to count how many items a collection has using the new Firebase database, Cloud Firestore?

If so, how do I do that?

17条回答
  •  心在旅途
    2020-11-22 09:54

    A workaround is to:

    write a counter in a firebase doc, which you increment within a transaction everytime you create a new entry

    You store the count in a field of your new entry (i.e: position: 4).

    Then you create an index on that field (position DESC).

    You can do a skip+limit with a query.Where("position", "<" x).OrderBy("position", DESC)

    Hope this helps!

提交回复
热议问题