docs/docs/en/api/database/operators.md
Used in the filter parameter of APIs like find, findOne, findAndCount, count of a Repository:
const repository = db.getRepository('books');
repository.find({
filter: {
title: {
$eq: 'The Great Gatsby',
},
},
});
To support JSON serialization, NocoBase identifies query operators with a string prefixed with $.
Additionally, NocoBase provides an API to extend operators, see db.registerOperators() for details.
$eqChecks if the field value is equal to the specified value. Equivalent to SQL's =.
Example
repository.find({
filter: {
title: {
$eq: 'The Great Gatsby',
},
},
});
Equivalent to title: 'The Great Gatsby'.
$neChecks if the field value is not equal to the specified value. Equivalent to SQL's !=.
Example
repository.find({
filter: {
title: {
$ne: 'The Great Gatsby',
},
},
});
$isChecks if the field value is the specified value. Equivalent to SQL's IS.
Example
repository.find({
filter: {
title: {
$is: null,
},
},
});
$notChecks if the field value is not the specified value. Equivalent to SQL's IS NOT.
Example
repository.find({
filter: {
title: {
$not: null,
},
},
});
$colChecks if the field value is equal to the value of another field. Equivalent to SQL's =.
Example
repository.find({
filter: {
title: {
$col: 'name',
},
},
});
$inChecks if the field value is in the specified array. Equivalent to SQL's IN.
Example
repository.find({
filter: {
title: {
$in: ['The Great Gatsby', 'Moby Dick'],
},
},
});
$notInChecks if the field value is not in the specified array. Equivalent to SQL's NOT IN.
Example
repository.find({
filter: {
title: {
$notIn: ['The Great Gatsby', 'Moby Dick'],
},
},
});
$emptyChecks if a general field is empty. For a string field, it checks for an empty string. For an array field, it checks for an empty array.
Example
repository.find({
filter: {
title: {
$empty: true,
},
},
});
$notEmptyChecks if a general field is not empty. For a string field, it checks for a non-empty string. For an array field, it checks for a non-empty array.
Example
repository.find({
filter: {
title: {
$notEmpty: true,
},
},
});
$andLogical AND. Equivalent to SQL's AND.
Example
repository.find({
filter: {
$and: [{ title: 'The Book of Songs' }, { isbn: '1234567890' }],
},
});
$orLogical OR. Equivalent to SQL's OR.
Example
repository.find({
filter: {
$or: [{ title: 'The Book of Songs' }, { publishedAt: { $lt: '0000-00-00T00:00:00Z' } }],
},
});
For boolean fields type: 'boolean'
$isFalsyChecks if a boolean field value is falsy. Field values of false, 0, and NULL are all considered $isFalsy: true.
Example
repository.find({
filter: {
isPublished: {
$isFalsy: true,
},
},
});
$isTrulyChecks if a boolean field value is truly. Field values of true and 1 are all considered $isTruly: true.
Example
repository.find({
filter: {
isPublished: {
$isTruly: true,
},
},
});
For numeric fields, including:
type: 'integer'type: 'float'type: 'double'type: 'real'type: 'decimal'$gtChecks if the field value is greater than the specified value. Equivalent to SQL's >.
Example
repository.find({
filter: {
price: {
$gt: 100,
},
},
});
$gteChecks if the field value is greater than or equal to the specified value. Equivalent to SQL's >=.
Example
repository.find({
filter: {
price: {
$gte: 100,
},
},
});
$ltChecks if the field value is less than the specified value. Equivalent to SQL's <.
Example
repository.find({
filter: {
price: {
$lt: 100,
},
},
});
$lteChecks if the field value is less than or equal to the specified value. Equivalent to SQL's <=.
Example
repository.find({
filter: {
price: {
$lte: 100,
},
},
});
$betweenChecks if the field value is between the two specified values. Equivalent to SQL's BETWEEN.
Example
repository.find({
filter: {
price: {
$between: [100, 200],
},
},
});
$notBetweenChecks if the field value is not between the two specified values. Equivalent to SQL's NOT BETWEEN.
Example
repository.find({
filter: {
price: {
$notBetween: [100, 200],
},
},
});
For string fields, including string
$includesChecks if the string field contains the specified substring.
Example
repository.find({
filter: {
title: {
$includes: 'Classic',
},
},
});
$notIncludesChecks if the string field does not contain the specified substring.
Example
repository.find({
filter: {
title: {
$notIncludes: 'Classic',
},
},
});
$startsWithChecks if the string field starts with the specified substring.
Example
repository.find({
filter: {
title: {
$startsWith: 'Classic',
},
},
});
$notStatsWithChecks if the string field does not start with the specified substring.
Example
repository.find({
filter: {
title: {
$notStatsWith: 'Classic',
},
},
});
$endsWithChecks if the string field ends with the specified substring.
Example
repository.find({
filter: {
title: {
$endsWith: 'Classic',
},
},
});
$notEndsWithChecks if the string field does not end with the specified substring.
Example
repository.find({
filter: {
title: {
$notEndsWith: 'Classic',
},
},
});
$likeChecks if the field value contains the specified string. Equivalent to SQL's LIKE.
Example
repository.find({
filter: {
title: {
$like: 'Computer',
},
},
});
$notLikeChecks if the field value does not contain the specified string. Equivalent to SQL's NOT LIKE.
Example
repository.find({
filter: {
title: {
$notLike: 'Computer',
},
},
});
$iLikeChecks if the field value contains the specified string, case-insensitive. Equivalent to SQL's ILIKE (PostgreSQL only).
Example
repository.find({
filter: {
title: {
$iLike: 'Computer',
},
},
});
$notILikeChecks if the field value does not contain the specified string, case-insensitive. Equivalent to SQL's NOT ILIKE (PostgreSQL only).
Example
repository.find({
filter: {
title: {
$notILike: 'Computer',
},
},
});
$regexpChecks if the field value matches the specified regular expression. Equivalent to SQL's REGEXP (PostgreSQL only).
Example
repository.find({
filter: {
title: {
$regexp: '^Computer',
},
},
});
$notRegexpChecks if the field value does not match the specified regular expression. Equivalent to SQL's NOT REGEXP (PostgreSQL only).
Example
repository.find({
filter: {
title: {
$notRegexp: '^Computer',
},
},
});
$iRegexpChecks if the field value matches the specified regular expression, case-insensitive. Equivalent to SQL's ~* (PostgreSQL only).
Example
repository.find({
filter: {
title: {
$iRegexp: '^COMPUTER',
},
},
});
$notIRegexpChecks if the field value does not match the specified regular expression, case-insensitive. Equivalent to SQL's !~* (PostgreSQL only).
Example
repository.find({
filter: {
title: {
$notIRegexp: '^COMPUTER',
},
},
});
For date fields type: 'date'
$dateOnChecks if the date field is on a specific day.
Example
repository.find({
filter: {
createdAt: {
$dateOn: '2021-01-01',
},
},
});
$dateNotOnChecks if the date field is not on a specific day.
Example
repository.find({
filter: {
createdAt: {
$dateNotOn: '2021-01-01',
},
},
});
$dateBeforeChecks if the date field is before a specific value. Equivalent to being less than the provided date value.
Example
repository.find({
filter: {
createdAt: {
$dateBefore: '2021-01-01T00:00:00.000Z',
},
},
});
$dateNotBeforeChecks if the date field is not before a specific value. Equivalent to being greater than or equal to the provided date value.
Example
repository.find({
filter: {
createdAt: {
$dateNotBefore: '2021-01-01T00:00:00.000Z',
},
},
});
$dateAfterChecks if the date field is after a specific value. Equivalent to being greater than the provided date value.
Example
repository.find({
filter: {
createdAt: {
$dateAfter: '2021-01-01T00:00:00.000Z',
},
},
});
$dateNotAfterChecks if the date field is not after a specific value. Equivalent to being less than or equal to the provided date value.
Example
repository.find({
filter: {
createdAt: {
$dateNotAfter: '2021-01-01T00:00:00.000Z',
},
},
});
For array fields type: 'array'
$matchChecks if the array field's value matches the values in the specified array.
Example
repository.find({
filter: {
tags: {
$match: ['Literature', 'History'],
},
},
});
$notMatchChecks if the array field's value does not match the values in the specified array.
Example
repository.find({
filter: {
tags: {
$notMatch: ['Literature', 'History'],
},
},
});
$anyOfChecks if the array field's value contains any of the values in the specified array.
Example
repository.find({
filter: {
tags: {
$anyOf: ['Literature', 'History'],
},
},
});
$noneOfChecks if the array field's value contains none of the values in the specified array.
Example
repository.find({
filter: {
tags: {
$noneOf: ['Literature', 'History'],
},
},
});
$arrayEmptyChecks if the array field is empty.
Example
repository.find({
filter: {
tags: {
$arrayEmpty: true,
},
},
});
$arrayNotEmptyChecks if the array field is not empty.
Example
repository.find({
filter: {
tags: {
$arrayNotEmpty: true,
},
},
});
Used to check if an association exists. Field types include:
type: 'hasOne'type: 'hasMany'type: 'belongsTo'type: 'belongsToMany'$existsAssociation data exists
Example
repository.find({
filter: {
author: {
$exists: true,
},
},
});
$notExistsNo association data exists
Example
repository.find({
filter: {
author: {
$notExists: true,
},
},
});