Queries

Many list endpoints in Appwrite allow you to filter, sort, and paginate results using queries. Appwrite provides a common set of syntax to build queries.

Query class

Appwrite SDKs provide a Query class to help you build queries. The Query class has methods for each type of supported query operation.

Building queries

Queries are passed to an endpoint through the queries parameter as an array of query strings, which can be generated using the Query class.

Each query method is logically separated via AND operations. For OR operation, pass multiple values into the query method separated by commas. For example Query.equal('title', ['Avatar', 'Lord of the Rings']) will fetch the movies Avatar or Lord of the Rings.

Default pagination behavior

By default, results are limited to the first 25 items. You can change this through pagination.

Query operators

Select

The select operator allows you to specify which attributes should be returned from a document. This is useful for optimizing response size and only retrieving the data you need.

Comparison operators

Equal

Returns document if attribute is equal to any value in the provided array.

Not equal

Returns document if attribute is not equal to any value in the provided array.

Less than

Returns document if attribute is less than the provided value.

Less than or equal

Returns document if attribute is less than or equal to the provided value.

Greater than

Returns document if attribute is greater than the provided value.

Greater than or equal

Returns document if attribute is greater than or equal to the provided value.

Between

Returns document if attribute value falls between the two values. The boundary values are inclusive and can be strings or numbers.

Null checks

Is null

Returns documents where attribute value is null.

Is not null

Returns documents where attribute value is not null.

String operations

Starts with

Returns documents if a string attribute starts with a substring.

Ends with

Returns documents if a string attribute ends with a substring.

Contains

Returns documents if the array attribute contains the specified elements or if a string attribute contains the specified substring.

Searches string attributes for provided keywords. Requires a full-text index on queried attributes.

Logical operators

AND

Returns document if it matches all of the nested sub-queries in the array passed in.

OR

Returns document if it matches any of the nested sub-queries in the array passed in.

Ordering

Order descending

Orders results in descending order by attribute. Attribute must be indexed.

Order ascending

Orders results in ascending order by attribute. Attribute must be indexed.

Pagination

Limit

Limits the number of results returned by the query. Used for pagination.

Offset

Offset the results returned by skipping some of the results. Used for pagination.

Cursor after

Places the cursor after the specified resource ID. Used for pagination.

Cursor before

Places the cursor before the specified resource ID. Used for pagination.

Complex queries

You can create complex queries by combining AND and OR operations. For example, to find items that are either books under $20 or magazines under $10:

This example demonstrates how to combine OR and AND operations. The query uses Query.or() to match either condition: books under $20 OR magazines under $10. Each condition within the OR is composed of two AND conditions - one for the category and one for the price threshold. The database will return documents that match either of these combined conditions.