Ordering and limiting Firestore queries

Stay organized with collections Save and categorize content based on your preferences.

Ordering and limiting Firestore queries

Explore further

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

Code sample

Go

query := cities.OrderBy("name", firestore.Asc).Limit(3)
query := cities.OrderBy("name", firestore.Asc).LimitToLast(3)

Java

Query query = cities.orderBy("name").limit(3);
Query query = cities.orderBy("name").limitToLast(3);

Node.js

const firstThreeRes = await citiesRef.orderBy('name').limit(3).get();

PHP

$query = $citiesRef->orderBy('name')->limit(3);

Python

cities_ref = db.collection("cities")
query = cities_ref.order_by("name").limit_to_last(2)
results = query.get()

Ruby

query = cities_ref.order("name").limit(3)

What's next

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