This is not possible by default, however, you could keep a doc/collection to hold metadata associated with collections, in this specific case you can use a distributed counter.
The idea is to have a cloud function which listens for onWrite
events and increase or decrease the counter, I cannot recall the code exactly but it would be something like this:
const metadataCollectionRef = firebase.firestore().collection(`metadata`).doc(`users`)
export const myCollectionCounter = functions
.firestore.document(`users/{doc}`).onWrite(async (change, _context) => {
const oldData = change.before
const newData = change.after
const data = newData.data()
if (!oldData.exists && newData.exists) {
// creating
return metadataCollectionRef.set({
count: FieldValue.increment(1)
}, {merge: true})
} else if (!newData.exists && oldData.exists) {
// deleting
return metadataCollectionRef.set({
count: FieldValue.increment(-1)
}, {merge: true})
}
})
This should give you enough information to get started. Docs are here