GCP – Querying Firestore in the Cloud Console
Power up your Firestore queries in the console: we’re excited to announce the launch of the new query builder, adding several major enhancements to the experience. You can now visually construct queries using all the power of the Firestore SDK—query collections or collection groups using multiple WHERE clauses, equalities, and comparison operators. Query results are displayed in a table format to enhance data exploration. Let’s get to it!
Querying with multiple WHERE clauses
To see the new features, we’ll look at a fictional car rental agency’s “customers” collection. We want to find our US customers who’ve spent over $5,000 on rentals. From the query builder tab:
Click ‘Add to Query’ to add a WHERE clause
Choose ‘country’ as the field, and type ‘United States’ as the field value
Add a second WHERE clause
Choose ‘lifetime_value’ as the field, ‘>’ as the operator, ‘number’ as the data type, and ‘5000’ as the value.
Click ‘Run’
The query results are shown in the table and stay updated as the contents change. Each query you run is added to your browser history, making it easy to navigate, bookmark, and share your work.
What if we only want to see our top 5 US customers?
Change the second ‘WHERE’ clause to an ‘ORDER BY’ clause
Choose ‘lifetime_value’ as the field, and ‘descending’ as the order
Click ‘Add to Query’ to add a ‘LIMIT’ clause with a value of ‘5’
Click ‘Run’
The table now shows the 5 highest-spending customers in the US.
Querying subcollections
Let’s say we want to look at a particular customer’s rentals. Click the ‘View’ chip to update the query scope to a specific subcollection.
Querying collection groups
Collection group queries let you retrieve documents from all collections with the same ID. Let’s say we want to look at each customer’s rental history to find everyone who has rented a Subaru or GMC. For that, we need to query by collection group.
Change the ‘Query Scope’ dropdown to ‘Collection group’.
Enter ‘rentals’ as the collection group.
Click ‘Add to Query’ to add a WHERE clause.
Select ‘make’ as the field and ‘IN’ as the operator.
‘IN’ accepts an array of values to match on. Click the icon to edit this array and add string values for ‘Subaru’ and ‘GMC’.
Click ‘Update’ and then ‘Run’.
Get started
The query builder is now available in Firestore Native Mode, Datastore Mode, and the Firebase Console. Use the above examples to help get started or click to learn more about managing Firestore in the Google Cloud console. Happy querying!
Read More for the details.