Reputation: 1259
I tried CollectionReference
.count
as in the documentation here (admin.firestore().collection("users").count()
)
but it says
TypeError: collectionReference.count is not a function
I also tried: (admin.firestore().collection("users").countDocuments()
)
And still:
TypeError: collectionReference.countDocuments is not a function
Upvotes: 1
Views: 853
Reputation: 2835
To get the size of a collection, do this:
According to the Count docs: The server calculates the count, and transmits only the result, a single integer, back to your app, saving on both billed document reads and bytes transferred, compared to executing the full query.
// Below is in preview
const collectionRef = admin.firestore().collection('users');
const snapshot = await collectionRef.count().get();
console.log(snapshot.data().count);
// below will fetch all the data and count it. It is more expensive.
const snapshot = await admin.firestore().collection("users").get();
// your function should be async
console.log('collection size is: ', snapshot.size);
// or
console.log('collection size is: ', snapshot.docs.length);
You must fetch the collection before getting the length.
Upvotes: 2