Reading data
get
Fetch a single document by its ID.The ID of the document to fetch.
The document if it exists, or
null if it has been deleted or never existed.query
Begin a query for documents in a table.The name of the table to query.
A query builder object. See Query methods below.
normalizeId
Validate and normalize an ID string for a specific table.The name of the table.
The ID string to validate.
The normalized ID if valid, or
null if the ID is invalid for this table.Query methods
withIndex
Query documents using an index for efficient lookups.The name of the index to use.
A function that builds an index range using equality and comparison operators:
q.eq(field, value)- Match documents where field equals valueq.gt(field, value)- Greater thanq.gte(field, value)- Greater than or equalq.lt(field, value)- Less thanq.lte(field, value)- Less than or equal
withSearchIndex
Perform full-text search using a search index.The name of the search index.
A function that defines the search query:
q.search(field, text)- Full-text search on the search fieldq.eq(field, value)- Filter by exact match on filter fields
order
Define the order of query results.The sort order:
"asc" for ascending or "desc" for descending.filter
Filter query results based on a condition..withIndex() instead of .filter() whenever possible. Filters scan all documents matched so far, while indexes efficiently skip non-matching documents.
A function that returns a filter expression using:
q.eq(a, b)- Equalq.neq(a, b)- Not equalq.lt(a, b)- Less thanq.lte(a, b)- Less than or equalq.gt(a, b)- Greater thanq.gte(a, b)- Greater than or equalq.and(...)- Logical ANDq.or(...)- Logical ORq.not(expr)- Logical NOTq.field(name)- Reference a field
Consuming query results
collect
Return all query results as an array..collect() when the result set is tightly bounded. For large or unbounded result sets, use .take(n), .first(), .unique(), or pagination.
take
Return the firstn results.
The maximum number of results to return.
first
Return the first result, ornull if there are no results.
unique
Return the only result, throwing an error if there are multiple matches.paginate
Load a page of results with a cursor for loading more.The documents in this page.
A cursor to fetch the next page.
Whether this is the last page.
Async iteration
Process results one at a time without loading all into memory:Writing data
Write operations are only available in mutations (MutationCtx).
insert
Insert a new document into a table.The name of the table.
The document to insert. Do not include system fields (
_id, _creationTime).The ID of the newly inserted document.
patch
Shallow merge updates into an existing document.The ID of the document to update.
The fields to update. Fields set to
undefined are removed.patch for partial updates. Use replace to overwrite the entire document.
replace
Completely replace a document with new values.The ID of the document to replace.
The new document. System fields (
_id, _creationTime) are preserved automatically.delete
Delete a document from the database.The ID of the document to delete.
System tables
Access system tables like_storage using ctx.db.system:
_storage- File metadata_scheduled_functions- Scheduled function state
Best practices
Use indexes for queries
Always use
.withIndex() instead of .filter() for field-based queries. Indexes are dramatically faster.Avoid unbounded queries
Don’t use
.collect() on queries that can return unlimited results. Use .take(n), pagination, or async iteration.Prefer patch over replace
Use
.patch() for partial updates to avoid accidentally removing fields.Check for null
Always check if
.get(), .first(), or .unique() returns null before using the result.