Filtering
Filtering, sorting, and pagination guide
Filtering
A filtering reference for use with the SDK and REST API.
Pagination
const { docs, totalDocs, totalPages, hasNextPage } = await client.from('posts').find({
page: 1,
limit: 20,
})Sorting
await client.from('posts').find({ sort: 'title' }) // Ascending
await client.from('posts').find({ sort: '-createdAt' }) // Descending
await client.from('posts').find({ sort: '-_status,title' }) // Multiple sortWhere Operators
| Operator | Description | Example |
|---|---|---|
equals | Equal to | { _status: { equals: 'published' } } |
not_equals | Not equal to | { _status: { not_equals: 'draft' } } |
greater_than | Greater than | { publishedAt: { greater_than: '2024-01-01' } } |
greater_than_equal | Greater than or equal to | { publishedAt: { greater_than_equal: '2024-01-01' } } |
less_than | Less than | { publishedAt: { less_than: '2025-01-01' } } |
less_than_equal | Less than or equal to | { publishedAt: { less_than_equal: '2025-01-01' } } |
in | In list | { _status: { in: ['published', 'draft'] } } |
not_in | Not in list | { _status: { not_in: ['draft'] } } |
like | Partial match | { title: { like: 'guide' } } |
contains | Array contains | { tags: { contains: 'featured' } } |
exists | Field exists | { thumbnail: { exists: true } } |
Logical Operators
AND
await client.from('posts').find({
where: {
and: [
{ _status: { equals: 'published' } },
{ publishedAt: { greater_than: '2024-01-01' } },
]
}
})OR
await client.from('posts').find({
where: {
or: [
{ _status: { equals: 'published' } },
{ _status: { equals: 'draft' } },
]
}
})Compound Conditions
await client.from('posts').find({
where: {
and: [
{ _status: { equals: 'published' } },
{
or: [
{ publishedAt: { greater_than: '2024-06-01' } },
{ tags: { contains: 'featured' } },
]
}
]
}
})Relationship Filtering
// Posts in a specific category
await client.from('posts').find({
where: { categories: { equals: 'category-id' } }
})
// Filter by relationship field property
await client.from('posts').find({
where: { 'category.slug': { equals: 'tech' } }
})Date Filtering
await client.from('posts').find({
where: {
publishedAt: {
greater_than: '2024-01-01T00:00:00.000Z',
less_than: new Date().toISOString(),
}
}
})REST API Query Strings
The same filtering works in REST API via URL query parameters.
# Basic filtering
GET /api/posts?where[_status][equals]=published
# Compound conditions
GET /api/posts?where[and][0][_status][equals]=published&where[and][1][publishedAt][greater_than]=2024-01-01
# OR conditions
GET /api/posts?where[or][0][_status][equals]=published&where[or][1][_status][equals]=draft
# Pagination + sorting
GET /api/posts?page=1&limit=20&sort=-createdAt
# Relationship filtering
GET /api/posts?where[categories][equals]=category-id
# in operator
GET /api/posts?where[_status][in][0]=published&where[_status][in][1]=draft