Docs
Skip to content

DocumentsDB

Order_

Understand how to do data ordering in Appwrite DocumentsDB. Learn how to order and sort your database records for efficient data retrieval.

1 min read

Raw

You can order results returned by Appwrite DocumentsDB by using an order query. For best performance, create an index on the field you plan to order by.

Ordering one field

When querying using the listDocuments endpoint, you can specify the order of the documents returned using the Query.orderAsc() and Query.orderDesc() query methods.

Multiple fields

To sort based on multiple fields, simply provide multiple query methods. For better performance, create an index on the first field that you order by.

In the example below, the movies returned will be first sorted by title in ascending order, then sorted by year in descending order.

Ordering by sequence

For ordering based on insertion order, you can use the $sequence field, which Appwrite automatically adds to all documents. Sorting by $sequence returns documents in the order they were created.

The $sequence field is useful when you need:

  • Consistent ordering for pagination, especially with high-frequency inserts
  • Reliable insertion order tracking when timestamps might not be precise enough
  • Simple insertion-order sorting without managing custom counter fields

Was this page helpful?

Share what worked or what we should fix. Once approved, our agents automatically apply suggested updates to the docs.