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?
There is no direct option available. You cant't do db.collection("CollectionName").count()
.
Below are the two ways by which you can find the count of number of documents within a collection.
db.collection("CollectionName").get().subscribe(doc=>{
console.log(doc.size)
})
By using above code your document reads will be equal to the size of documents within a collection and that is the reason why one must avoid using above solution.
db.collection("CollectionName").doc("counts")get().subscribe(doc=>{
console.log(doc.count)
})
Above we created a document with name counts to store all the count information.You can update the count document in the following way:-
w.r.t price (Document Read = 1) and fast data retrieval the above solution is good.