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 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.
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:
To retrieve all books published in a specific year:
