Sort, Filter, and Order Firestore Queries

While working with large data, we may need to sort, order, or filter the documents in a collection to be used in our application. In Cloud Firestore, we can create a database query to specify the documents we want to retrieve in a collection. To create a Cloud Firestore query, we must use the query function imported from the firebase/firestore subpackage. This function takes a collection reference as its first parameter, followed by a comma-separated list of query constraints to be applied.

Note: The Cloud Firestore query function is entirely different from the Realtime Database query function.

Use the where function

The where function is creates simple Cloud Firestore queries. It takes in three parameters: the path to compare, the operation string, and the value for comparison. These parameters enforce that the documents returned in the query snapshot must contain the compared path as a field. Its value must satisfy the operation string provided.

The query snapshot can be handled the same way we handle the getDocs function–by iterating with a forEach method:

Get hands-on with 1200+ tech skills courses.