I'm using the following code to retrieve a random document from a firestore collection:
collection = db.collection('Items')
total_docs = collection.count().get()[0][0].value
random_offset = random.randint(0, total_docs - 1)
random_doc = collection.limit(1).offset(random_offset).get()[0]
I noticed that this code produces a lot of read-usage and discovered that the entire collection is read when counting the documents.
Hence my question: How can retrieve the count of documents in a collection without reading the entire collection?
And if this isn't possible: How can I randomly retrieve a document from a collection without specifying how many documents the collection contains?
Many thanks!