Use the filter property when you need to narrow down your results based on dynamic criteria or specific field values. While Structural Querying defines where or what you are looking for, Filtering allows you to search based on content, such as authors, dates, or full-text search terms.
await cms.first({
type: BlogPostSchema,
// Filter using search terms
search: 'querying',
// Filter by fields
// This filters the blogposts with author 'alinea'
filter: {
author: 'alinea'
}
})When filtering, you can use these operators to create precise conditions. They are especially useful for dates, numbers, and strings.
is / isNot (Equality)
filter: {status:{is: 'published'}}
filter: {type: {isNot: 'feature'}}}in / notIn (List matching)
filter: {category: {in: ['news', 'tech']}}
filter: {tags: {notIn: ['draft', 'internal']}}gt / gte (Greater than (or equal))
filter: {price: {gt: 100}}
filter: {publishedDate: {gte: '2024-01-01'}}lt / lte (Less than (or equal))
filter: {stock: {lt: 10}}
filter: {date: {lte: today}}startsWith (String matching)
filter: {title: {startsWith: 'Alinea'}}or (Logical OR)
filter: {or: [{category: 'blog'}, {featured: true}]}Let's take using a date as a filter for example as this lets us show quite a few of your options.
const today = new Date().toISOString()
// Getting the posts published in 2023
await cms.find({
type: BlogPostSchema,
filter: {
author: 'alinea',
publishedDate: {
lt: '2024-01-01',
gte: '2023-01-01'
}
}
})
// Getting future events
await cms.find({
type: Event,
filter: {
date: {gte: today}
}
})