Docs
Skip to content

VectorsDB

Order_

Order documents returned by Appwrite VectorsDB. Learn how to sort by system fields like $createdAt and $sequence, and why metadata sub-fields can't be ordered.

2 min read

Raw

You can order the documents returned by listDocuments using the Query.orderAsc() and Query.orderDesc() query methods.

VectorsDB orders on the system fields that Appwrite maintains on every document, such as $createdAt, $updatedAt, $sequence, and $id.

Order by a system field

Pass Query.orderAsc() or Query.orderDesc() with a system field to sort the documents returned. The example below orders documents from newest to oldest by $createdAt.

Order by multiple fields

To sort by more than one field, pass multiple order queries. They are applied in order, so the first query is the primary sort and each later query breaks ties.

In the example below, documents are sorted first by $createdAt in descending order, then by $id in ascending order to break ties.

Order by sequence

For ordering based on insertion order, use the $sequence field, which Appwrite adds to every document. 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

Order randomly

Use Query.orderRandom() to return documents in a random order. This is useful for sampling documents or surfacing varied results on each request. It takes no field.

To rank documents by similarity to a query vector instead of ordering by a field, use a vector search query. See Vector search.

Was this page helpful?

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