Firestore Pagination (Python)

Apr 24, 2019
Loop query documents with limit and paging
limit = 10query = db.collection("test") \    .where("is_active", '==', True) \    .order_by("created", direction=firestore.Query.DESCENDING) \    .limit(limit)docs = query.stream()count = 0while True:    # docs is an iterator, not list    # docs = list(docs)    # last_doc = docs[-1]    # size = len(docs)}    doc = None    current_count = 0    for doc in docs:        current_count += 1        logging.info(f"{doc.id}, {doc.get('content')}")    last_doc = doc    count += current_count    if not last_doc or current_count < limit:        break    logging.info("next_page")    docs = query.start_after(last_doc).get()logging.info(f"count={count}")

NOTE: For paging to work, query must include order_by.

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.