The Query class provides a set of static methods to generate query strings used for filtering, searching, sorting, and paginating data in Appwrite. These queries are typically passed as a list of strings to service methods like databases.listDocuments().
Common Query Types
Comparison and Logic
equal(attribute, value): Matches where attribute equals value. If value is a list, it matches any value in that list.notEqual(attribute, value): Matches where attribute is not equal to value.lessThan(attribute, value), lessThanEqual(attribute, value), greaterThan(attribute, value), greaterThanEqual(attribute, value): Standard numeric/date comparisons.between(attribute, start, end): Matches where attribute is between start and end (inclusive).regex(attribute, pattern): Matches using a regular expression.or([queries]) and and([queries]): Logical grouping of multiple query strings.
String and Array Operations
startsWith(attribute, value) / endsWith(attribute, value) / contains(attribute, value): String pattern matching.exists(attributes) / notExists(attributes): Checks for the presence of specific attributes.containsAny(attribute, values): For arrays/relationships, matches if the attribute contains at least one of the values.containsAll(attribute, values): For arrays/relationships, matches if the attribute contains all of the values.elemMatch(attribute, queries): Filters array elements where at least one element matches all specified queries.
Metadata and System Queries
Appwrite provides built-in attributes for system metadata:
createdBefore(value), createdAfter(value), createdBetween(start, end): Filters by $createdAt.updatedBefore(value), updatedAfter(value), updatedBetween(start, end): Filters by $updatedAt.
Sorting and Pagination
orderAsc(attribute) / orderDesc(attribute): Sorts results.limit(int): Limits the number of returned results.offset(int): Skips a specific number of results.cursorBefore(id) / cursorAfter(id): Used for cursor-based pagination.
Geospatial and Vector Search
distanceEqual, distanceGreaterThan, etc.: Filters based on distance from coordinates.vectorDot, vectorCosine, vectorEuclidean: Performs vector similarity searches.intersects, crosses, overlaps, touches: Geometric intersection queries.
// Example: Fetching documents that are active and were created after a certain date
final documents = await databases.listDocuments(
databaseId: 'my_db',
collectionId: 'my_collection',
queries: [
Query.equal('status', 'active'),
Query.createdAfter('2023-01-01T00:00:00.000Z'),
Query.orderDesc('$createdAt'),
Query.limit(25),
],
);