Advanced Firestore Queries

Introduction to Querying in Firestore

Welcome back! In previous lessons, you learned how to retrieve individual documents and multiple documents from Firestore collections. Today, we'll explore more advanced ways to retrieve data by using queries and filters. These techniques allow you to efficiently find documents that match specific criteria, making it easier to work with large and complex datasets. For our examples, we'll use a books collection, where each document represents a book with fields such as year, title, and author.

Firestore Querying Model Overview

Firestore provides a powerful querying model that lets you filter documents within a collection based on field values. You can retrieve all documents that match one or more conditions, such as finding all books published in a certain year or by a specific author. Queries in Firestore are always performed within a single collection and can use a variety of comparison operators.

Unlike some databases, Firestore does not have a "scan" operation that reads every document in a collection. Instead, you use queries to retrieve only the documents that match your specified conditions, which is more efficient and scalable.

Crafting and Enhancing Queries

To create queries in Firestore, you use method chaining to add filters and select specific fields. Here are some common ways to build and execute queries:

Basic Query: Filtering by a Single Field

To retrieve all books published in a specific year:

from google.cloud import firestore
from google.cloud.firestore import FieldFilter

db = firestore.Client()
books_ref = db.collection('books')

query = books_ref.where(filter=FieldFilter('year', '==', 2018))
results = query.stream()

for doc in results:
    print(doc.id, doc.to_dict())

Output:

book_001 {'title': 'The Silent Patient', 'author': 'Alex Michaelides', 'year': 2018, 'genre': 'thriller'}
book_003 {'title': 'Educated', 'author': 'Tara Westover', 'year': 2018, 'genre': 'memoir'}
book_007 {'title': 'Becoming', 'author': 'Michelle Obama', 'year': 2018, 'genre': 'autobiography'}

Querying with Multiple Conditions

You can chain multiple where() calls with FieldFilter to filter documents by more than one field. For example, to find all books published in 2015 with a title that starts with "The":

query = books_ref.where(filter=FieldFilter('year', '==', 2015)).where(filter=FieldFilter('title', '>=', 'The')).where(filter=FieldFilter('title', '<', 'Thf'))
results = query.stream()

for doc in results:
    print(doc.id, doc.to_dict())

Output:

book_012 {'title': 'The Girl on the Train', 'author': 'Paula Hawkins', 'year': 2015, 'genre': 'thriller'}
book_018 {'title': 'The Martian', 'author': 'Andy Weir', 'year': 2015, 'genre': 'science fiction'}

Note: Firestore does not support "begins with" queries directly, but you can use range queries on strings to approximate this behavior.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal