Skip to main content
The DatabaseReader service is used to read documents from the database.

Retrieve a single document

By ID

By index

Single field

Multiple fields

Retrieve multiple documents

Indexes

Every query must specify either a standard index or a search index.

Standard indexes

Standard indexes are what you’ll use most of the time. There are two default standard indexes, which exist for every table: by_creation_time and by_id. The rest are defined in your schema. Standard indexes determine both the sort order of the results and which fields may be filtered on. Fields must be filtered in the order they are defined in the index. Order is ascending by default, but can be specified in the final argument to the index method.
Users sorted by name and age, ascending
Users sorted by name and age, descending
Users sorted by name and age, where name equals "John Doe", ascending
Users sorted by name and age, where name equals "John Doe", and age equals 21, descending

Search indexes

Search indexes are used for full-text search, and are defined in your schema. The results are always sorted by relevance, descending.

Methods

The following methods are available for queries using both database indexes and search indexes.

Collect

Collect all documents matching the query.

First

Retrieve the first document matching the query.

Take

Take the first n documents matching the query.

Paginate

Retrieve a page of documents matching the query. Expects a PaginationOptions object.

Stream

Get a Stream of documents matching the query.

Accessing the current time

Reading real time inside a query handler affects how aggressively Convex caches the result. See Determinism for how Confect keeps queries cacheable by default and how to opt in to real time via Effect’s Clock service.