查询条件
当你想进行复杂查询时,我们很容易得到大量充满大括号的样板代码:
¥When you want to make complex queries, we can easily end up with a lot of boilerplate code full of curly brackets:
const res = await orm.em.find(Author, { $and: [
{ id: { $in: [1, 2, 7] }, },
{ id: { $nin: [3, 4] }, },
{ id: { $gt: 5 }, },
{ id: { $lt: 10 }, },
{ id: { $gte: 7 }, },
{ id: { $lte: 8 }, },
{ id: { $ne: 9 }, },
] });
对于具有单个字段的 AND 条件,我们也可以这样做:
¥For AND condition with single field, we can also do this:
const res = await orm.em.find(Author, {
id: {
$in: [1, 2, 7],
$nin: [3, 4],
$gt: 5,
$lt: 10,
$gte: 7,
$lte: 8,
$ne: 9,
},
});
还有 $in
的快捷方式 - 只需提供数组作为值,它将自动转换:
¥There is also shortcut for $in
- simply provide array as value, and it will be converted automatically:
const res = await orm.em.find(Author, { favouriteBook: [1, 2, 7] });
对于主键查找,我们可以将数组直接提供给 em.find()
:
¥For primary key lookup, we can provide the array directly to em.find()
:
const res = await orm.em.find(Author, [1, 2, 7]);
支持的运算符列表
¥List of supported operators
比较
¥Comparison
operator | name | description |
---|---|---|
$eq | equals | 匹配等于指定值的值。 |
$gt | greater | 匹配大于指定值的值。 |
$gte | 大于或等于 | 匹配大于或等于指定值的值。 |
$in | contains | 匹配数组中指定的任何值。 |
$lt | lower | 匹配小于指定值的值。 |
$lte | 小于或等于 | 匹配小于或等于指定值的值。 |
$ne | 不相等 | 匹配所有不等于指定值的值。 |
$nin | 不包含 | 不匹配数组中指定的任何值。 |
$like | like | 使用 LIKE 运算符 |
$re | regexp | 使用 REGEXP 运算符。参见信息 below |
$fulltext | 全文 | 特定于驱动程序的全文搜索功能。参见要求 below |
$ilike | ilike | (仅限 postgres) |
$overlap | && | (仅限 postgres) |
$contains | @> | (仅限 postgres) |
$contained | <@ | (仅限 postgres) |
$hasKey | ? | (仅限 postgres) |
$hasSomeKeys | ?| | (仅限 postgres) |
$hasKeys | ?& | (仅限 postgres) |
逻辑
¥Logical
operator | description |
---|---|
$and | 使用逻辑 AND 连接查询子句返回与两个子句的条件匹配的所有文档。 |
$not | 反转查询表达式的效果并返回与查询表达式不匹配的文档。 |
$or | 使用逻辑或连接查询子句将返回与任一子句的条件匹配的所有文档。 |
集合
¥Collection
除了转换为真实 SQL 运算符表达式的常规运算符(例如 >=
)之外,你还可以使用以下集合运算符:
¥In addition to the regular operators that translate to a real SQL operator expression (e.g. >=
), you can also use the following collection operators:
operator | description |
---|---|
$some | 查找具有与条件匹配的记录的集合。 |
$none | 查找没有与条件匹配的记录的集合。 |
$every | 查找每个记录都与条件匹配的集合。 |
这将作为子查询条件解决:
¥This will be resolved as a subquery condition:
// finds all authors that have some book called `Foo`
const res1 = await em.find(Author, {
books: { $some: { title: 'Foo' } },
});
// finds all authors that have no books called `Foo`
const res2 = await em.find(Author, {
books: { $none: { title: 'Foo' } },
});
// finds all authors that have every book called `Foo`
const res3 = await em.find(Author, {
books: { $every: { title: 'Foo' } },
});
条件对象也可以为空:
¥The condition object can be also empty:
// finds all authors that have at least one book
const res1 = await em.find(Author, {
books: { $some: {} },
});
// finds all authors that have no books
const res2 = await em.find(Author, {
books: { $none: {} },
});
正则表达式
¥Regular Expressions
$re
运算符将字符串作为输入值,默认情况下使用区分大小写的运算符。如果你想要使用 RegExp
对象,即能够设置标志,则直接在字段名称上搜索而不使用运算符:
¥The $re
operator takes a string as input value, and by default uses the case-sensitive operator. If you would like to use a RegExp
object, i.e. to be able to set flags, then search directly on the field name without using the operator:
const res = await em.find(Painter, {
lastName: /m[oa]net/i, // or `new RegExp('m[oa]net', 'i')`
});
全文搜索
¥Full text searching
全文搜索是指在存储的大量文本数据中搜索一些文本并返回包含查询中的部分或全部单词的结果。相比之下,传统搜索会返回完全匹配。
¥Full-text search refers to searching some text inside extensive text data stored and returning results that contain some or all of the words from the query. In contrast, traditional search would return exact matches.
每个驱动程序的实现和要求不同,因此正确设置字段非常重要。
¥The implementation and requirements differs per driver so it's important that fields are set up correctly.
PostgreSQL
PosgreSQL 允许在 pg-vector 类型上执行查询 (pg-query)。pg-vector 类型可以是列(性能更高),也可以在查询中创建(数据库中没有多余的列)。使用列时,还支持 自定义 regconfig
或 setweight
(默认 regconfig
为 simple
)等高级功能。
¥PosgreSQL allows to execute queries (pg-query) on the type pg-vector. The pg-vector type can be a column (more performant) or be created in the query (no excess columns in the database). When using a column, advanced functionality such as a custom regconfig
or setweight
(the default regconfig
is simple
) is also supported.
有关可能的查询,请参阅 PostgreSQL 文档。
¥Refer to the PostgreSQL documentation for possible queries.
- Using a column
- Using an index
import { FullTextType, WeightedFullTextValue } from '@mikro-orm/postgresql';
@Entity()
export class Book {
@Property()
title!: string;
// example when using default settings
@Index({ type: 'fulltext' })
@Property({ type: FullTextType, onUpdate: (book) => book.title })
searchableTitle!: string;
// example when using a custom regconfig
@Index({ type: 'fulltext' })
@Property({ type: new FullTextType('english'), onUpdate: (book) => book.title })
searchableTitle!: string;
// example when using weights
@Index({ type: 'fulltext' })
@Property({ type: FullTextType, onUpdate: (book) => ({ A: book.title, B: book.description }) })
searchableTitle!: WeightedFullTextValue;
}
找到结果:repository.findOne({ searchableTitle: { $fulltext: 'query' } })
。
¥And to find results: repository.findOne({ searchableTitle: { $fulltext: 'query' } })
.
@Entity()
export class Book {
@Index({ type: 'fulltext' })
@Property()
title!: string;
}
找到结果:repository.findOne({ title: { $fulltext: 'query' } })
¥And to find results: repository.findOne({ title: { $fulltext: 'query' } })
MySQL、MariaDB
MySQL 和 MariaDB 允许使用全文索引对所有列进行全文搜索。
¥MySQL and MariaDB allow full text searches on all columns with a fulltext index.
有关可能的查询,请参阅 MySQL 文档 或 MariaDB 文档。
¥Refer to the MySQL documentation or MariaDB documentation for possible queries.
@Entity()
export class Book {
@Index({ type: 'fulltext' })
@Property()
title!: string;
}
找到结果:repository.findOne({ title: { $fulltext: 'query' } })
¥And to find results: repository.findOne({ title: { $fulltext: 'query' } })
MongoDB
MongoDB 允许使用文本索引对所有列进行全文搜索。但是,在执行全文搜索时,它会根据具有文本索引的所有字段选择匹配项:只能添加一个查询,并且只能在查询对象的顶层添加。有关此行为的更多信息,请参阅 MongoDB 文档。
¥MongoDB allows full text searches on all columns with a text index. However, when executing a full text search, it selects matches based on all fields with a text index: it's only possible to add one query and only on the top-level of the query object. Refer to the MongoDB documentation for more information on this behavior.
有关可能的查询,请参阅 MongoDB 文档。
¥Refer to the MongoDB documentation for possible queries.
@Entity()
export class Book {
@Index({ type: 'fulltext' })
@Property()
title!: string;
}
SQLite
在 SQLite 中,只能在 FTS5 虚拟表 上执行全文搜索。MikroORM 无法创建此表,必须完成 manually。同样,你可以使用文本属性并将其前 1000 个字符存储为描述:
¥In SQLite, full text searches can only be executed on FTS5 virtual tables. MikroORM can't create this table, and has to be done manually. Simple tables can be created with this query:
CREATE VIRTUAL TABLE <table name> USING fts5(<column1>, <column2>, ...);
之后,可以正常为此表的结构创建一个实体。@Index
对于 SQLite 中的全文搜索不是必需的。
¥Afterwards an entity can create normally for the structure of this table. The @Index
is not necessary for full text searches in SQLite.
有关可能的查询,请参阅 SQLite 文档。
¥Refer to the SQLite documentation for possible queries.
@Entity()
export class Book {
@PrimaryKey()
id!: number;
@Property()
title!: string;
}
找到结果:repository.findOne({ title: { $fulltext: 'query' } })
¥And to find results: repository.findOne({ title: { $fulltext: 'query' } })