Cloud Firestore collection count

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

    Simplest way to do so is to read the size of a "querySnapshot".

    db.collection("cities").get().then(function(querySnapshot) {      
        console.log(querySnapshot.size); 
    });
    

    You can also read the length of the docs array inside "querySnapshot".

    querySnapshot.docs.length;
    

    Or if a "querySnapshot" is empty by reading the empty value, which will return a boolean value.

    querySnapshot.empty;
    

提交回复
热议问题