Order and Limit Data with Cloud Firestore

Cloud Firestore provides powerful query functionality for specifying which documents you want to retrieve from a collection. These queries can also be used with either get() or addSnapshotListener(), as described in Get Data.

Order and limit data

Cloud Firestore also lets you specify the sort order for your data and specify a limit to how many documents you want to retrieve using orderBy() and limit(). For example, you could query for the first 3 cities alphabetically with:

Web
citiesRef.orderBy("name").limit(3)
Swift
citiesRef.order(by: "name").limit(to: 3)
Objective-C
[[citiesRef queryOrderedByField:@"name"] queryLimitedTo:3];
  
Android
citiesRef.orderBy("name").limit(3);
Java
Query query = cities.orderBy("name").limit(3);
Python
db.collection(u'cities').order_by(u'name').limit(3).get()
Node.js
var firstThree = citiesRef.orderBy('name').limit(3);
Go
query := cities.OrderBy("name", firestore.Asc).Limit(3)

You could also sort in descending order to get the last 3 cities:

Web
citiesRef.orderBy("name", "desc").limit(3)
Swift
citiesRef.order(by: "name", descending: true).limit(to: 3)
Objective-C
[[citiesRef queryOrderedByField:@"name" descending:YES] queryLimitedTo:3];
  
Android
citiesRef.orderBy("name", Direction.DESCENDING).limit(3);
Java
Query query = cities.orderBy("name", Direction.DESCENDING).limit(3);
Python
cities_ref = db.collection(u'cities')
query = cities_ref.order_by(
    u'name', direction=firestore.Query.DESCENDING).limit(3)
results = query.get()
Node.js
var lastThree = citiesRef.orderBy('name', 'desc').limit(3);
Go
query := cities.OrderBy("name", firestore.Desc).Limit(3)

You can also order by multiple fields. For example, if you wanted to order by state, and within each state order by population in descending order:

Web
citiesRef.orderBy("state").orderBy("population", "desc")
Swift
citiesRef
    .order(by: "state")
    .order(by: "population", descending: true)
Objective-C
[[citiesRef queryOrderedByField:@"state"] queryOrderedByField:@"population" descending:YES];
  
Android
citiesRef.orderBy("state").orderBy("population", Direction.DESCENDING);
Java
Query query = cities.orderBy("state").orderBy("population", Direction.DESCENDING);
Python
cities_ref = db.collection(u'cities')
cities_ref.order_by(u'state').order_by(
    u'population', direction=firestore.Query.DESCENDING)
Node.js
var byStateByPop = citiesRef.orderBy('state').orderBy('population', 'desc');
Go
query := client.Collection("cities").OrderBy("state", firestore.Asc).OrderBy("population", firestore.Desc)

You can combine where() filters with orderBy() and limit(). In the following example, the queries define a population threshold, sort by population in ascending order, and return only the first few results that exceed the threshold:

Web
citiesRef.where("population", ">", 100000).orderBy("population").limit(2)
Swift
citiesRef
    .whereField("population", isGreaterThan: 100000)
    .order(by: "population")
    .limit(to: 2)
Objective-C
[[[citiesRef queryWhereField:@"population" isGreaterThan:@100000]
    queryOrderedByField:@"population"]
    queryLimitedTo:2];
  
Android
citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2);
Java
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population").limit(2);
Python
cities_ref = db.collection(u'cities')
query = cities_ref.where(
    u'population', u'>', 2500000).order_by(u'population').limit(2)
results = query.get()
Node.js
var biggest = citiesRef.where('population', '>', 2500000).orderBy('population').limit(2);
Go
query := cities.Where("population", ">", 2500000).OrderBy("population", firestore.Desc).Limit(2)

However, if you have a filter with a range comparison (<, <=, >, >=), your first ordering must be on the same field:

Valid: Range filter and orderBy on the same field

Web
citiesRef.where("population", ">", 100000).orderBy("population")
Swift
citiesRef
    .whereField("population", isGreaterThan: 100000)
    .order(by: "population")
Objective-C
[[citiesRef queryWhereField:@"population" isGreaterThan:@100000]
    queryOrderedByField:@"population"];
  
Android
citiesRef.whereGreaterThan("population", 100000).orderBy("population");
Java
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population");
Python
cities_ref = db.collection(u'cities')
query = cities_ref.where(
    u'population', u'>', 2500000).order_by(u'population')
results = query.get()
Node.js
citiesRef.where('population', '>', 2500000).orderBy('population');
Go
query := cities.Where("population", ">", 2500000).OrderBy("population", firestore.Asc)

Invalid: Range filter and first orderBy on different fields

Web
citiesRef.where("population", ">", 100000).orderBy("country")
Swift
citiesRef
    .whereField("population", isGreaterThan: 100000)
    .order(by: "country")
Objective-C
[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"country"];
  
Android
citiesRef.whereGreaterThan("population", 100000).orderBy("country");
Java
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("country");
Python
cities_ref = db.collection(u'cities')
query = cities_ref.where(u'population', u'>', 2500000).order_by(u'country')
results = query.get()
Node.js
citiesRef.where('population', '>', 2500000).orderBy('country');
Go
// Note: This is an invalid query. It violates the constraint that range
// and order by are required to be on the same field.
query := cities.Where("population", ">", 2500000).OrderBy("country", firestore.Asc)

Send feedback about...

Need help? Visit our support page.