Skip to main content

Querying Data

note

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 CollectQuery 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 Collect.records.find('author', {
where: {
$AND: [
{ name: { $startsWith: 'Jane' } },
{ email: { $contains: '@example.com' } }
]
}
});
// Basic example without $AND
const queryWithAnd = await Collect.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 Collect.records.find('post', {
where: {
$OR: [
{ rating: { $gte: 4 } },
{ title: { $contains: 'Guide' } }
]
}
});
// Complex example with $AND and $OR for numbers
const queryComplexNumber = await Collect.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 Collect.records.find('author', {
where: {
$NOT: [
{ email: { $contains: '@example.com' } }
]
}
});

$XOR

// Example using $XOR
const queryWithXor = await Collect.records.find('author', {
where: {
$XOR: [
{ name: { $startsWith: 'Jane' } },
{ email: { $contains: '@example.com' } }
]
}
});

The $XOR operator (exclusive OR) combines multiple conditions and returns results that match one and only one of the conditions.

Examples:

Comparison Operators

Comparison operators are used to filter records based on specific field values.

Boolean Operators

$not

The $not operator checks if a field is not equal to a specified value. This operator is implicitly used when specifying field values directly.

Examples:
const queryNotFalse = await Collect.records.find('author', {
where: {
email: { $startsWith: '' },
married: { $not: false }
}
});

Datetime Operators

$gt

The $gt (greater than) operator checks if a field's value is greater than the specified datetime value.

Examples:
const queryGreaterDatetime = await Collect.records.find('post', {
where: {
created: { $gt: { $year: 2023, $month: 1, $day: 1 } }
}
});
// Finds posts created after January 1, 2023

$gte

The $gte (greater than or equal to) operator checks if a field's value is greater than or equal to the specified datetime value.

Examples:
const queryGreaterOrEqualDatetime = await Collect.records.find('post', {
where: {
created: { $gte: '2023-01-01T00:00:00Z' }
}
});
// Finds posts created on or after January 1, 2023, 00:00:00 UTC

$lt

The $lt (less than) operator checks if a field's value is less than the specified value.

Examples:
const queryLesserDatetime = await Collect.records.find('post', {
where: {
created: { $lt: { $year: 2024, $month: 1, $day: 1 } }
}
});
// Finds posts created before January 1, 2024

$lte

The $lte (less than or equal to) operator checks if a field's value is less than or equal to the specified value.

Examples:
// Complex example with $gte and $lte for datetime
const queryWithDatetime = await Collect.records.find('post', {
where: {
created: { $gte: '2023-01-01T00:00:00Z', $lte: '2023-12-31T23:59:59Z' }
}
});
// Example using $lte for datetime as object
const queryWithLteDatetimeObject = await Collect.records.find('post', {
where: {
created: { $lte: { $year: 2024, $month: 1, $day: 1 } }
}
});

$not

The $not operator is used to find records where the datetime field does not match the specified value.

Examples:
const queryNotDatetime = await Collect.records.find('post', {
where: {
created: { $not: '2023-01-01T00:00:00Z' }
}
});
// Finds posts not created on January 1, 2023, 00:00:00 UTC

$notIn

The $notIn operator is used to find records where the datetime field does not match any value in the specified array.

Examples:
const queryNotInDatetime = await Collect.records.find('post', {
where: {
created: { $notIn: [
{ $year: 2023, $month: 1, $day: 1 },
{ $year: 2023, $month: 2, $day: 1 }
]}
}
});
// Finds posts not created on January 1, 2023 or February 1, 2023

$in

The $in operator is used to find records where the datetime field matches any value in the specified array.

Examples:
const queryInDatetime = await Collect.records.find('post', {
where: {
created: { $in: [
'2023-01-01T00:00:00Z',
'2023-02-01T00:00:00Z'
]}
}
});
// Finds posts created on January 1, 2023 or February 1, 2023

Number Operators

$gt

The $gt (greater than) operator checks if a field's value is greater than the specified value.

Examples:
// Example using $gt
const queryWithGt = await Collect.records.find('post', {
where: {
rating: { $gt: 4 }
}
});

$gte

The $gte (greater than or equal to) operator checks if a field's value is greater than or equal to the specified value.

Examples:
// Example using $gte
const queryWithGte = await Collect.records.find('post', {
where: {
rating: { $gte: 4 }
}
});

$lt

The $lt (less than) operator checks if a field's value is less than the specified value.

Examples:
// Example using $lt
const queryWithLt = await Collect.records.find('post', {
where: {
rating: { $lt: 4 }
}
});

$lte

The $lte (less than or equal to) operator checks if a field's value is less than the specified value.

Examples:
// Example using $lte
const queryWithLte = await Collect.records.find('post', {
where: {
rating: { $lte: 4 }
}
});

$in

The $in operator checks if a field's value is within a specified array of values.

Examples:
// Example using $in (numbers)
const queryWithInNumbers = await Collect.records.find('author', {
where: {
age: { $in: [25, 30, 35] }
}
});

$notIn

The $notIn operator checks if a field's value is not within a specified array of values.

Examples:
// Example using $notIn (numbers)
const queryWithNotInNumbers = await Collect.records.find('author', {
where: {
age: { $notIn: [25, 30, 35] }
}
});

String Operators

$contains

The $contains operator checks if a string field contains the specified substring.

Examples:
// Example using $contains
const queryWithContains = await Collect.records.find('post', {
where: {
content: { $contains: 'Graph' }
}
});

$endsWith

The $endsWith operator checks if a string field ends with the specified substring.

Examples:
// Example using $endsWith
const queryWithEndsWith = await Collect.records.find('post', {
where: {
title: { $endsWith: 'Databases' }
}
});

$startsWith

The $startsWith operator checks if a string field starts with the specified substring.

Examples:
// Example using $startsWith
const queryWithStartsWith = await Collect.records.find('post', {
where: {
title: { $startsWith: 'Understanding' }
}
});
// Complex example with multiple string operators
const queryWithStringOperators = await Collect.records.find('post', {
where: {
$OR: [
{ title: { $startsWith: 'Understanding' } },
{ title: { $contains: 'Graph' } },
{ title: { $endsWith: 'Databases' } }
]
}
});

$in

The $in operator checks if a field's value is within a specified array of values.

Examples:
// Example using $in (strings)
const queryWithInStrings = await Collect.records.find('author', {
where: {
name: { $in: ['Jane Doe', 'John Smith'] }
}
});

$notIn

The $notIn operator checks if a field's value is not within a specified array of values.

Examples:
// Example using $notIn (strings)
const queryWithNotInStrings = await Collect.records.find('author', {
where: {
name: { $notIn: ['Jane Doe', 'John Smith'] }
}
});

Complex examples

// Complex example with nested queries
const queryWithNested = await Collect.records.find('author', {
where: {
name: { $startsWith: 'Jane' },
blog: {
$AND: [
{ title: { $contains: 'Tech' } },
{ post: { rating: { $gte: 4 } } }
]
}
}
});
// Example with nested relation and logical operators
const nestedQuery = await Collect.records.find('author', {
where: {
name: { $startsWith: 'Post author' },
blog: {
$AND: [
{ title: { $contains: 'Tech' } },
{ post: { $OR: [{ rating: { $gte: 4 } }, { rating: { $lte: 2 } }] } }
]
}
}
});
// Complex example with $not and $notIn
const queryWithEqAndNotIn = await Collect.records.find('author', {
where: {
married: { $not: false },
age: { $notIn: [20, 25, 30] }
}
});
// Complex example with $gt for number and datetime
const queryWithGtComplex = await Collect.records.find('post', {
where: {
rating: { $gt: 3 },
created: { $gt: { $year: 2023, $month: 1, $day: 1 } }
}
});
// Basic example with $gte
const queryWithGteDatetime = await Collect.records.find('post', {
where: {
created: { $gte: { $year: 2023, $month: 1, $day: 1 } }
}
});
// Complex example with $notIn for string and number
const queryWithNotInStringNumber = await Collect.records.find('author', {
where: {
name: { $notIn: ['Jane Doe', 'John Doe'] },
age: { $notIn: [30, 40, 50] }
}
});
// Complex example with $in for string and number
const queryWithInStringNumber = await Collect.records.find('author', {
where: {
name: { $in: ['Jane Doe', 'John Doe'] },
age: { $in: [30, 40, 50] }
}
});
// Complex example with $not and $notIn
const queryWithEqAndNotIn = await Collect.records.find('author', {
where: {
married: { $not: false },
age: { $notIn: [20, 25, 30] }
}
});
// Complex example with multiple string operators
const queryWithStringOperators = await Collect.records.find('post', {
where: {
$OR: [
{ title: { $startsWith: 'Understanding' } },
{ title: { $contains: 'Graph' } },
{ title: { $endsWith: 'Databases' } }
]
}
});
// Complex example with nested queries
const queryWithNested = await Collect.records.find('author', {
where: {
name: { $startsWith: 'Jane' },
blog: {
$AND: [
{ title: { $contains: 'Tech' } },
{ post: { rating: { $gte: 4 } } }
]
}
}
});

Notes

  • You can use both CollectModel and Collect class instances to perform queries.
  • Using logical operators allows building complex and precise queries.
  • The examples provided showcase how to leverage these operators for querying data effectively.