Order and limit data with Cloud Firestore Stay organized with collections Save and categorize content based on your preferences.
Cloud Firestore provides powerful query functionality for specifying whichdocuments you want to retrieve from a collection. These queries can also be usedwith eitherget() oraddSnapshotListener(), as described inGetData.
Order and limit data
By default, a query retrieves all documents that satisfy the query in ascendingorder by document ID. You can specify the sort order for your data usingorderBy(), and you can limit the number of documents retrieved usinglimit(). If you specify alimit(), the value must be greater than or equalto zero.
orderBy() clause alsofilters for existence of the given field.The result set will not include documents that do not contain the given field.For example, you could query for the first 3 cities alphabeticallywith:
Web
import{query,orderBy,limit}from"firebase/firestore";constq=query(citiesRef,orderBy("name"),limit(3));
Web
Learn more about the tree-shakeable modular Web API and its advantages over the namespaced API.citiesRef.orderBy("name").limit(3);
Swift
citiesRef.order(by:"name").limit(to:3)
Objective-C
[[citiesRefqueryOrderedByField:@"name"]queryLimitedTo:3];
Kotlin
citiesRef.orderBy("name").limit(3)
Java
citiesRef.orderBy("name").limit(3);
Dart
finalcitiesRef=db.collection("cities");citiesRef.orderBy("name").limit(3);
Java
Queryquery=cities.orderBy("name").limit(3);Queryquery=cities.orderBy("name").limitToLast(3);Python
cities_ref=db.collection("cities")query=cities_ref.order_by("name").limit_to_last(2)results=query.get()Python
cities_ref=db.collection("cities")query=cities_ref.order_by("name").limit_to_last(2)results=awaitquery.get()C++
cities_ref.OrderBy("name").Limit(3);
Node.js
constfirstThreeRes=awaitcitiesRef.orderBy('name').limit(3).get();Go
query:=cities.OrderBy("name",firestore.Asc).Limit(3)query:=cities.OrderBy("name",firestore.Asc).LimitToLast(3)PHP
PHP
For more on installing and creating aCloud Firestore client, refer toCloud Firestore Client Libraries.
$query = $citiesRef->orderBy('name')->limit(3);Unity
Queryquery=citiesRef.OrderBy("Name").Limit(3);
C#
Queryquery=citiesRef.OrderBy("Name").Limit(3);Ruby
query=cities_ref.order("name").limit(3)You could also sort in descending order to get thelast 3 cities:
Web
import{query,orderBy,limit}from"firebase/firestore";constq=query(citiesRef,orderBy("name","desc"),limit(3));
Web
Learn more about the tree-shakeable modular Web API and its advantages over the namespaced API.citiesRef.orderBy("name","desc").limit(3);
Swift
citiesRef.order(by:"name",descending:true).limit(to:3)
Objective-C
[[citiesRefqueryOrderedByField:@"name"descending:YES]queryLimitedTo:3];
Kotlin
citiesRef.orderBy("name",Query.Direction.DESCENDING).limit(3)
Java
citiesRef.orderBy("name",Direction.DESCENDING).limit(3);
Dart
finalcitiesRef=db.collection("cities");citiesRef.orderBy("name",descending:true).limit(3);
Java
Queryquery=cities.orderBy("name",Direction.DESCENDING).limit(3);Python
cities_ref=db.collection("cities")query=cities_ref.order_by("name",direction=firestore.Query.DESCENDING).limit(3)results=query.stream()Python
cities_ref=db.collection("cities")query=cities_ref.order_by("name",direction=firestore.Query.DESCENDING).limit(3)results=query.stream()C++
cities_ref.OrderBy("name",Query::Direction::kDescending).Limit(3);
Node.js
constlastThreeRes=awaitcitiesRef.orderBy('name','desc').limit(3).get();Go
query:=cities.OrderBy("name",firestore.Desc).Limit(3)PHP
PHP
For more on installing and creating aCloud Firestore client, refer toCloud Firestore Client Libraries.
$query = $citiesRef->orderBy('name', 'DESC')->limit(3);Unity
Queryquery=citiesRef.OrderByDescending("Name").Limit(3);
C#
Queryquery=citiesRef.OrderByDescending("Name").Limit(3);Ruby
query=cities_ref.order("name","desc").limit(3)You can also order by multiple fields. For example, if you wanted to order bystate, and within each state order by population in descending order:
Web
import{query,orderBy}from"firebase/firestore";constq=query(citiesRef,orderBy("state"),orderBy("population","desc"));
Web
Learn more about the tree-shakeable modular Web API and its advantages over the namespaced API.citiesRef.orderBy("state").orderBy("population","desc");
Swift
citiesRef.order(by:"state").order(by:"population",descending:true)
Objective-C
[[citiesRefqueryOrderedByField:@"state"]queryOrderedByField:@"population"descending:YES];
Kotlin
citiesRef.orderBy("state").orderBy("population",Query.Direction.DESCENDING)
Java
citiesRef.orderBy("state").orderBy("population",Direction.DESCENDING);
Dart
finalcitiesRef=db.collection("cities");citiesRef.orderBy("state").orderBy("population",descending:true);
Java
Queryquery=cities.orderBy("state").orderBy("population",Direction.DESCENDING);Python
cities_ref=db.collection("cities")ordered_city_ref=cities_ref.order_by("state").order_by("population",direction=firestore.Query.DESCENDING)Python
cities_ref=db.collection("cities")cities_ref.order_by("state").order_by("population",direction=firestore.Query.DESCENDING)C++
cities_ref.OrderBy("state").OrderBy("name",Query::Direction::kDescending);
Node.js
constbyStateByPopRes=awaitcitiesRef.orderBy('state').orderBy('population','desc').get();Go
query:=client.Collection("cities").OrderBy("state",firestore.Asc).OrderBy("population",firestore.Desc)PHP
PHP
For more on installing and creating aCloud Firestore client, refer toCloud Firestore Client Libraries.
$query = $citiesRef->orderBy('state')->orderBy('population', 'DESC');Unity
Queryquery=citiesRef.OrderBy("State").OrderByDescending("Population");
C#
Queryquery=citiesRef.OrderBy("State").OrderByDescending("Population");Ruby
query=cities_ref.order("state").order("population","desc")You can combinewhere() filters withorderBy() andlimit(). In thefollowing example, the queries define a population threshold, sort by populationin ascending order, and return only the first few results that exceed thethreshold:
Web
import{query,where,orderBy,limit}from"firebase/firestore";constq=query(citiesRef,where("population",">",100000),orderBy("population"),limit(2));
Web
Learn more about the tree-shakeable modular Web API and its advantages over the namespaced API.citiesRef.where("population",">",100000).orderBy("population").limit(2);
Swift
citiesRef.whereField("population",isGreaterThan:100000).order(by:"population").limit(to:2)
Objective-C
[[[citiesRefqueryWhereField:@"population"isGreaterThan:@100000]queryOrderedByField:@"population"]queryLimitedTo:2];
Kotlin
citiesRef.whereGreaterThan("population",100000).orderBy("population").limit(2)
Java
citiesRef.whereGreaterThan("population",100000).orderBy("population").limit(2);
Dart
finalcitiesRef=db.collection("cities");citiesRef.where("population",isGreaterThan:100000).orderBy("population").limit(2);
Java
Queryquery=cities.whereGreaterThan("population",2500000L).orderBy("population").limit(2);Python
cities_ref=db.collection("cities")query=(cities_ref.where(filter=FieldFilter("population",">",2500000)).order_by("population").limit(2))results=query.stream()Python
cities_ref=db.collection("cities")query=(cities_ref.where(filter=FieldFilter("population",">",2500000)).order_by("population").limit(2))results=query.stream()C++
cities_ref.WhereGreaterThan("population",FieldValue::Integer(100000)).OrderBy("population").Limit(2);
Node.js
constbiggestRes=awaitcitiesRef.where('population','>',2500000).orderBy('population').limit(2).get();Go
query:=cities.Where("population",">",2500000).OrderBy("population",firestore.Desc).Limit(2)PHP
PHP
For more on installing and creating aCloud Firestore client, refer toCloud Firestore Client Libraries.
$query = $citiesRef ->where('population', '>', 2500000) ->orderBy('population') ->limit(2);Unity
Queryquery=citiesRef.WhereGreaterThan("Population",2500000).OrderBy("Population").Limit(2);
C#
Queryquery=citiesRef.WhereGreaterThan("Population",2500000).OrderBy("Population").Limit(2);Ruby
query=cities_ref.where("population",">",2_500_000).order("population").limit(2)However, if you have a filter with a range comparison (<,<=,>,>=),your first ordering must be on the same field, see the list oforderBy()limitations below.
Limitations
Note the following restriction fororderBy() clauses:
- An
orderBy()clause alsofilters for existence of the given fields. The result set will not include documents that do not contain the given fields.
orderBy and existence
When you order a query by a given field, the query can return only thedocuments where the order-by field exists.
For example, the following query would not return any documents where thepopulation field is not set, even if they otherwise meet the query filters.
Java
db.collection("cities").whereEqualTo("country",“USA”).orderBy(“population”);
A related effect applies to inequalities. A query with an inequality filteron a field also implies ordering by that field. The followingquery does not return documents without apopulation field evenifcountry = USA in that document . As a workaround, you can executeseparate queries for each ordering or you can assign a value for all fieldsthat you order by.
Java
db.collection(“cities”).where(or(“country”,USA”),greaterThan(“population”,250000));
The query above includes an implied order-by on the inequality and isequivalent to the following:
Java
db.collection(“cities”).where(or(“country”,USA”),greaterThan(“population”,250000)).orderBy(“population”);
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-02-18 UTC.