Ordering a Firestore query on multiple fields

Ordering a Firestore query on multiple fields

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample

Go

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

Java

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

Node.js

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

PHP

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

Python

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

Ruby

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