tachybase_todo/docs/en-US/api/database/operators.md

816 lines
11 KiB
Markdown
Raw Normal View History

# Filter Operators
2023-01-27 23:06:31 +08:00
Used in the filter parameters of the `find`, `findOne`, `findAndCount`, `count`, etc. APIs of repository:
```ts
const repository = db.getRepository('books');
repository.find({
filter: {
title: {
2023-01-27 23:02:33 +08:00
$eq: 'Spring and Autumn',
}
}
});
```
2023-01-27 23:02:33 +08:00
To support JSON, NocoBase identifies query operators as a string prefixed with $.
2023-01-27 23:02:33 +08:00
Moreover, NocoBase provides API to extend operators. Refer to [`db.registerOperators()`](../database#registeroperators).
2023-01-27 23:02:33 +08:00
## General Operators
### `$eq`
2023-01-27 23:02:33 +08:00
Check if the field value is equal to the specified value. Equivalent to `=` in SQL.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
title: {
2023-01-27 23:02:33 +08:00
$eq: 'Spring and Autumn',
}
}
});
```
2023-01-27 23:02:33 +08:00
Equal to `title: 'Spring and Autumn'`
### `$ne`
2023-01-27 23:02:33 +08:00
Check if the field value is not equal to the specified value. Equivalent to `!=` in SQL.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
title: {
2023-01-27 23:02:33 +08:00
$ne: 'Spring and Autumn',
}
}
});
```
### `$is`
2023-01-27 23:02:33 +08:00
Check if the field value is the specified value. Equivalent to `IS` in SQL.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
title: {
$is: null,
}
}
});
```
### `$not`
2023-01-27 23:02:33 +08:00
Check if the field value is not the specified value. Equivalent to `IS NOT` in SQL.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
title: {
$not: null,
}
}
});
```
### `$col`
2023-01-27 23:02:33 +08:00
Check if the field value is equal to the value of another field. Equivalent to `=` in SQL.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
title: {
$col: 'name',
}
}
});
```
### `$in`
2023-01-27 23:02:33 +08:00
Check if the field value is in the specified array. Equivalent to `IN` in SQL.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
title: {
2023-01-27 23:02:33 +08:00
$in: ['Spring and Autumn', 'Warring States'],
}
}
});
```
### `$notIn`
2023-01-27 23:02:33 +08:00
Check if the field value is not in the specified array. Equivalent to `NOT IN` in SQL.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
title: {
2023-01-27 23:02:33 +08:00
$notIn: ['Spring and Autumn', 'Warring States'],
}
}
});
```
### `$empty`
2023-01-27 23:02:33 +08:00
Check if the general field is empty. For string field, check if it is an empty string; for array field, check if it is an empty array.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
title: {
$empty: true,
}
}
});
```
### `$notEmpty`
2023-01-27 23:02:33 +08:00
Check if the general field is not empty. For string field, check if it is not an empty string; for array field, check if it is not an empty array.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
title: {
$notEmpty: true,
}
}
});
```
2023-01-27 23:02:33 +08:00
## Logical Operators
### `$and`
2023-01-27 23:02:33 +08:00
Logical AND. Equivalent to `AND` in SQL.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
$and: [
2023-01-27 23:02:33 +08:00
{ title: 'Book of Songs' },
{ isbn: '1234567890' },
]
}
});
```
### `$or`
2023-01-27 23:02:33 +08:00
Logical OR. Equivalent to `OR` in SQL.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
$or: [
2023-01-27 23:02:33 +08:00
{ title: 'Book of Songs' },
{ publishedAt: { $lt: '0000-00-00T00:00:00Z' } },
]
}
});
```
2023-01-27 23:02:33 +08:00
## Boolean Field Operators
2023-01-27 23:02:33 +08:00
For boolean fields: `type: 'boolean'`
### `$isFalsy`
2023-01-27 23:02:33 +08:00
Check if a Boolean field value is false. Boolean field values of `false`, `0` and `NULL` are all judged to be `$isFalsy: true`.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
isPublished: {
$isFalsy: true,
}
}
})
```
### `$isTruly`
2023-01-27 23:02:33 +08:00
Check if a Boolean field value is true. Boolean field values of `true` and `1` are all judged to be `$isTruly: true`.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
isPublished: {
$isTruly: true,
}
}
})
```
2023-01-27 23:02:33 +08:00
## Numeric Type Field Operators
2023-01-27 23:02:33 +08:00
For numeric type fields, including:
- `type: 'integer'`
- `type: 'float'`
- `type: 'double'`
- `type: 'real'`
- `type: 'decimal'`
### `$gt`
2023-01-27 23:02:33 +08:00
Check if the field value is greater than the specified value. Equivalent to `>` in SQL.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
price: {
$gt: 100,
}
}
});
```
### `$gte`
2023-01-27 23:02:33 +08:00
Check if the field value is equal to or greater than the specified value. Equivalent to `>=` in SQL.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
price: {
$gte: 100,
}
}
});
```
### `$lt`
2023-01-27 23:02:33 +08:00
Check if the field value is less than the specified value. Equivalent to `<` in SQL.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
price: {
$lt: 100,
}
}
});
```
### `$lte`
2023-01-27 23:02:33 +08:00
Check if the field value is equal to or less than the specified value. Equivalent to `<=` in SQL.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
price: {
$lte: 100,
}
}
});
```
### `$between`
2023-01-27 23:02:33 +08:00
Check if the field value is between the specified two values. Equivalent to `BETWEEN` in SQL.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
price: {
$between: [100, 200],
}
}
});
```
### `$notBetween`
2023-01-27 23:02:33 +08:00
Check if the field value is not between the specified two values. Equivalent to `NOT BETWEEN` in SQL.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
price: {
$notBetween: [100, 200],
}
}
});
```
2023-01-27 23:02:33 +08:00
## String Type Field Operators
2023-01-27 23:02:33 +08:00
For string type fields, including `string`.
### `$includes`
2023-01-27 23:02:33 +08:00
Check if the string field contains the specified substring.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
title: {
2023-01-27 23:02:33 +08:00
$includes: 'Three Character Classic',
}
}
})
```
### `$notIncludes`
2023-01-27 23:02:33 +08:00
Check if the string field does not contain the specified substring.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
title: {
2023-01-27 23:02:33 +08:00
$notIncludes: 'Three Character Classic',
}
}
})
```
### `$startsWith`
2023-01-27 23:02:33 +08:00
Check if the string field starts with the specified substring.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
title: {
2023-01-27 23:02:33 +08:00
$startsWith: 'Three Character Classic',
}
}
})
```
### `$notStatsWith`
2023-01-27 23:02:33 +08:00
Check if the string field does not start with the specified substring.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
title: {
2023-01-27 23:02:33 +08:00
$notStatsWith: 'Three Character Classic',
}
}
})
```
### `$endsWith`
2023-01-27 23:02:33 +08:00
Check if the string field ends with the specified substring.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
title: {
2023-01-27 23:02:33 +08:00
$endsWith: 'Three Character Classic',
}
}
})
```
### `$notEndsWith`
2023-01-27 23:02:33 +08:00
Check if the string field does not end with the specified substring.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
title: {
2023-01-27 23:02:33 +08:00
$notEndsWith: 'Three Character Classic',
}
}
})
```
### `$like`
2023-01-27 23:02:33 +08:00
Check if the field value contains the specified string. Equivalent to `LIKE` in SQL.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
title: {
2023-01-27 23:02:33 +08:00
$like: 'Computer',
}
}
});
```
### `$notLike`
2023-01-27 23:02:33 +08:00
Check if the field value does not contain the specified string. Equivalent to `NOT LIKE` in SQL.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
title: {
2023-01-27 23:02:33 +08:00
$notLike: 'Computer',
}
}
});
```
### `$iLike`
2023-01-27 23:02:33 +08:00
Check if a field value contains the specified string, case ignored. Equivalent to `ILIKE` in SQL (PG only).
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
title: {
$iLike: 'Computer',
}
}
});
```
### `$notILike`
2023-01-27 23:02:33 +08:00
Check if a field value does not contain the specified string, case ignored. Equivalent to `NOT ILIKE` in SQL (PG only).
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
title: {
$notILike: 'Computer',
}
}
});
```
### `$regexp`
2023-01-27 23:02:33 +08:00
Check if the field value matches the specified regular expression. Equivalent to `REGEXP` in SQL (PG only).
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
title: {
2023-01-27 23:02:33 +08:00
$regexp: '^Computer',
}
}
});
```
### `$notRegexp`
2023-01-27 23:06:31 +08:00
Check if the field value does not match the specified regular expression. Equivalent to `NOT REGEXP` in SQL (PG only).
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
title: {
2023-01-27 23:02:33 +08:00
$notRegexp: '^Computer',
}
}
});
```
### `$iRegexp`
2023-01-27 23:02:33 +08:00
Check if the field value matches the specified regular expression, case ignored. Equivalent to `~*` in SQL (PG only).
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
title: {
$iRegexp: '^COMPUTER',
}
}
});
```
### `$notIRegexp`
2023-01-27 23:06:31 +08:00
Check if the field value does not match the specified regular expression, case ignored. Equivalent to `!~*` in SQL (PG only).
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
title: {
$notIRegexp: '^COMPUTER',
}
}
});
```
2023-01-27 23:02:33 +08:00
## Date Type Field Operators
2023-01-27 23:02:33 +08:00
For date type fields: `type: 'date'`
### `$dateOn`
2023-01-27 23:02:33 +08:00
Check if the date field value is within a certain day.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
createdAt: {
$dateOn: '2021-01-01',
}
}
})
```
### `$dateNotOn`
2023-01-27 23:02:33 +08:00
Check if the date field value is not within a certain day.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
createdAt: {
$dateNotOn: '2021-01-01',
}
}
})
```
### `$dateBefore`
2023-01-27 23:06:31 +08:00
Check if the date field value is before a certain value, i.e., less than the one passed in.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
createdAt: {
$dateBefore: '2021-01-01T00:00:00.000Z',
}
}
})
```
### `$dateNotBefore`
2023-01-27 23:06:31 +08:00
Check if the date field value is not before a certain value, i.e., equal to or greater than the one passed in.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
createdAt: {
$dateNotBefore: '2021-01-01T00:00:00.000Z',
}
}
})
```
### `$dateAfter`
2023-01-27 23:06:31 +08:00
Check if the date field value is after a certain value, i.e., greater than the one passed in.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
createdAt: {
$dateAfter: '2021-01-01T00:00:00.000Z',
}
}
})
```
### `$dateNotAfter`
2023-01-27 23:06:31 +08:00
Check if the date field value is not after a certain value, i.e., equal to or greater than the one passed in.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
createdAt: {
$dateNotAfter: '2021-01-01T00:00:00.000Z',
}
}
})
```
2023-01-27 23:02:33 +08:00
## Array Type Field Operators
2023-01-27 23:02:33 +08:00
For array type fields: `type: 'array'`
### `$match`
2023-01-27 23:02:33 +08:00
Check if the array field values match values of the specified array.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
tags: {
2023-01-27 23:02:33 +08:00
$match: ['literature', 'history'],
}
}
})
```
### `$notMatch`
2023-01-27 23:02:33 +08:00
Check if the array field values do not match values of the specified array.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
tags: {
2023-01-27 23:02:33 +08:00
$notMatch: ['literature', 'history'],
}
}
})
```
### `$anyOf`
2023-01-27 23:02:33 +08:00
Check if the array field values contain any of the values of the specified array.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
tags: {
2023-01-27 23:02:33 +08:00
$anyOf: ['literature', 'history'],
}
}
})
```
### `$noneOf`
2023-01-27 23:02:33 +08:00
Check if the array field values contain none of the values of the specified array.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
tags: {
2023-01-27 23:02:33 +08:00
$noneOf: ['literature', 'history'],
}
}
})
```
### `$arrayEmpty`
2023-01-27 23:02:33 +08:00
Check if the array field is empty.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
tags: {
$arrayEmpty: true,
}
}
});
```
### `$arrayNotEmpty`
2023-01-27 23:02:33 +08:00
Check if the array field is not empty.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
tags: {
$arrayNotEmpty: true,
}
}
});
```
2023-01-27 23:02:33 +08:00
## Relational Field Type Operators
2023-01-27 23:02:33 +08:00
For checking if a relationship exists, field types include:
- `type: 'hasOne'`
- `type: 'hasMany'`
- `type: 'belongsTo'`
- `type: 'belongsToMany'`
### `$exists`
2023-01-27 23:02:33 +08:00
There is relational data existing.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
author: {
$exists: true,
}
}
});
```
### `$notExists`
2023-01-27 23:02:33 +08:00
There is no relational data existing.
2023-01-27 23:02:33 +08:00
**Example**
```ts
repository.find({
filter: {
author: {
$notExists: true,
}
}
});
```