Create a Firestore sharded counter (async)

Stay organized with collections Save and categorize content based on your preferences.

Create a Firestore sharded counter (async).

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

Python

async def init_counter(self, doc_ref):
    """
    Create a given number of shards as
    subcollection of specified document.
    """
    col_ref = doc_ref.collection("shards")

    # Initialize each shard with count=0
    for num in range(self._num_shards):
        shard = Shard()
        await col_ref.document(str(num)).set(shard.to_dict())

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.