Jump to Content
Developers & Practitioners

Querying Firestore in the Cloud Console

October 6, 2022
https://storage.googleapis.com/gweb-cloudblog-publish/images/firestore-query-hero.max-2600x2600.png
Oskar Perskaas

Front End Software Engineer

Doug Hungarter

Staff Interaction Designer

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:

  1. Click ‘Add to Query’ to add a WHERE clause

  2. Choose ‘country’ as the field, and type ‘United States’ as the field value

  3. Add a second WHERE clause

  4. Choose ‘lifetime_value’ as the field, ‘>’ as the operator, ‘number’ as the data type, and ‘5000’ as the value.

  5. 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.

https://storage.googleapis.com/gweb-cloudblog-publish/images/firestore-query-01.max-2200x2200.png

What if we only want to see our top 5 US customers?

  1. Change the second ‘WHERE’ clause to an ‘ORDER BY’ clause

  2. Choose ‘lifetime_value’ as the field, and ‘descending’ as the order

  3. Click ‘Add to Query’ to add a ‘LIMIT’ clause with a value of ‘5’

  4. Click ‘Run’

The table now shows the 5 highest-spending customers in the US.

https://storage.googleapis.com/gweb-cloudblog-publish/images/firestore-query-02.max-2200x2200.png

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.

https://storage.googleapis.com/gweb-cloudblog-publish/images/firestore-query-03.max-600x600.png

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.

  1. Change the ‘Query Scope’ dropdown to ‘Collection group’.

  2. Enter ‘rentals’ as the collection group.

  3. Click ‘Add to Query’ to add a WHERE clause.

  4. Select ‘make’ as the field and ‘IN’ as the operator.

  5. ‘IN’ accepts an array of values to match on. Click the icon to edit this array and add string values for ‘Subaru’ and ‘GMC’. 

  6. Click ‘Update’ and then ‘Run’.

https://storage.googleapis.com/gweb-cloudblog-publish/images/firestore-query-04.max-1200x1200.png
https://storage.googleapis.com/gweb-cloudblog-publish/images/firestore-query-05.max-2000x2000.png

Get started

The query builder is now available in both Firestore Native and Datastore Modes. Use the above examples to help get started or click to learn more about managing Firestore in the Google Cloud console. Happy querying!

Posted in