Cloud Firestore collection count

后端 未结 17 1312
庸人自扰
庸人自扰 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

    As far as I know there is no build-in solution for this and it is only possible in the node sdk right now. If you have a

    db.collection('someCollection')
    

    you can use

    .select([fields])
    

    to define which field you want to select. If you do an empty select() you will just get an array of document references.

    example:

    db.collection('someCollection').select().get().then( (snapshot) => console.log(snapshot.docs.length) );

    This solution is only a optimization for the worst case of downloading all documents and does not scale on large collections!

    Also have a look at this:
    How to get a count of number of documents in a collection with Cloud Firestore

提交回复
热议问题