Ordering a Firestore query on multiple fields

Ordering a Firestore query on multiple fields

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

C#

To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

Query query = citiesRef.OrderBy("State").OrderByDescending("Population");

Go

To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

query := client.Collection("cities").OrderBy("state", firestore.Asc).OrderBy("population", firestore.Desc)

Java

To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

Query query = cities.orderBy("state").orderBy("population", Direction.DESCENDING);

Node.js

To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

const byStateByPopRes = await citiesRef.orderBy('state').orderBy('population', 'desc').get();

PHP

To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

$query = $citiesRef->orderBy('state')->orderBy('population', 'DESC');

Python

To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

cities_ref = db.collection("cities")
ordered_city_ref = cities_ref.order_by("state").order_by(
    "population", direction=firestore.Query.DESCENDING
)

Ruby

To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

query = cities_ref.order("state").order("population", "desc")

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.