Querying Data
In previous sections, you have encountered the where
condition and various logical operators like $and
and $xor
. This section provides a comprehensive guide on querying data using the SearchQuery
type, covering all available logical and comparison operators.
Table of Contents
Logical Operators
Logical operators allow you to build complex queries by combining multiple conditions.
$and
The $and
operator combines multiple conditions and returns results that match all the conditions.
Alternatively, you can omit $and
and directly list the conditions if there are no other logical operators at the same level.
Examples:
// Basic example with $and
const queryWithAnd = await db.records.find('author', {
where: {
$and: [
{ name: { $startsWith: 'Jane' } },
{ email: { $contains: '@example.com' } }
]
}
});
// Basic example without $and
const queryWithAnd = await db.records.find('author', {
where: {
name: { $startsWith: 'Jane' },
email: { $contains: '@example.com' }
}
});
$or
The $or
operator combines multiple conditions and returns results that match any of the conditions. This is useful for querying records that meet at least one of several criteria.
Examples:
// Complex example with $or
const queryWithOr = await db.records.find('post', {
where: {
$or: [
{ rating: { $gte: 4 } },
{ title: { $contains: 'Guide' } }
]
}
});
// Complex example with $and and $or for numbers
const queryComplexNumber = await db.records.find('post', {
where: {
$and: [
{ rating: { $gte: 3, $lte: 5 } },
{ views: { $gt: 1000 } }
],
$or: [
{ comments: { $lt: 50 } },
{ shares: { $gte: 100 } }
]
}
});
$not
The $not
operator inverts the condition it applies to, returning results that do not match the specified condition.
Examples:
// Example using $not
const queryWithNot = await db.records.find('author', {
where: {
$not: [
{ email: { $contains: '@example.com' } }
]
}
});
$nor
The $nor
operator is used to combine multiple conditions and returns results that do not match any of the conditions.
Examples:
// Example using $nor
const queryWithNor = await db.records.find('author', {
where: {
$nor: [
{ name: { $startsWith: 'Jane' } },
{ email: { $contains: '@example.com' } }
]
}
});