# 查找选项

> 英文标题：Find Options
> 官网原文：https://typeorm.io/docs/working-with-entity-manager/find-options
> 文档版本：TypeORM v1.1
> 体例：英文原文（EN）与中文译文（ZH）逐块并列，共 163 块

---

<!-- EN -->
# Find Options

<!-- ZH -->
# 查找选项

<!-- EN -->
## Basic options

<!-- ZH -->
## 基础选项

<!-- EN -->
All repository and manager `.find*` methods accept special options you can use to query data you need without using `QueryBuilder`:

<!-- ZH -->
所有 repository 和 manager 的 `.find*` 方法都接受一些特殊选项，让你不必使用 `QueryBuilder` 就能查询到所需数据：

<!-- EN -->
* `select` - indicates which properties of the main object must be selected

<!-- ZH -->
* `select` - 表示主对象的哪些属性必须被选中

<!-- EN -->
```typescript
userRepository.find({
    select: {
        firstName: true,
        lastName: true,
    },
})
```

<!-- ZH -->
```typescript
userRepository.find({
    select: {
        firstName: true,
        lastName: true,
    },
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT "firstName", "lastName" FROM "user"
```

<!-- ZH -->
```sql
SELECT "firstName", "lastName" FROM "user"
```

<!-- EN -->
* `relations` - relations needs to be loaded with the main entity. Sub-relations can also be loaded (shorthand for `join` and `leftJoinAndSelect`)

<!-- ZH -->
* `relations` - 需要随主实体一起加载的关系。子关系也可以被加载（相当于 `join` 和 `leftJoinAndSelect` 的简写）

<!-- EN -->
```typescript
userRepository.find({
    relations: {
        profile: true,
        photos: true,
        videos: true,
    },
})
userRepository.find({
    relations: {
        profile: true,
        photos: true,
        videos: {
            videoAttributes: true,
        },
    },
})
```

<!-- ZH -->
```typescript
userRepository.find({
    relations: {
        profile: true,
        photos: true,
        videos: true,
    },
})
userRepository.find({
    relations: {
        profile: true,
        photos: true,
        videos: {
            videoAttributes: true,
        },
    },
})
```

<!-- EN -->
will execute following queries:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "user"
LEFT JOIN "profile" ON "profile"."id" = "user"."profileId"
LEFT JOIN "photos" ON "photos"."id" = "user"."photoId"
LEFT JOIN "videos" ON "videos"."id" = "user"."videoId"

SELECT * FROM "user"
LEFT JOIN "profile" ON "profile"."id" = "user"."profileId"
LEFT JOIN "photos" ON "photos"."id" = "user"."photoId"
LEFT JOIN "videos" ON "videos"."id" = "user"."videoId"
LEFT JOIN "video_attributes" ON "video_attributes"."id" = "videos"."video_attributesId"
```

<!-- ZH -->
```sql
SELECT * FROM "user"
LEFT JOIN "profile" ON "profile"."id" = "user"."profileId"
LEFT JOIN "photos" ON "photos"."id" = "user"."photoId"
LEFT JOIN "videos" ON "videos"."id" = "user"."videoId"

SELECT * FROM "user"
LEFT JOIN "profile" ON "profile"."id" = "user"."profileId"
LEFT JOIN "photos" ON "photos"."id" = "user"."photoId"
LEFT JOIN "videos" ON "videos"."id" = "user"."videoId"
LEFT JOIN "video_attributes" ON "video_attributes"."id" = "videos"."video_attributesId"
```

<!-- EN -->
* `relationLoadStrategy` - controls how relations are loaded: `"join"` (default) uses SQL JOINs, `"query"` uses separate queries. See [Eager and Lazy Relations](https://typeorm.io/docs/relations/eager-and-lazy-relations#relation-load-strategy) for details.

<!-- ZH -->
* `relationLoadStrategy` - 控制关系如何加载：`"join"`（默认）使用 SQL JOIN，`"query"` 使用独立查询。详见[急加载与懒加载关系](https://typeorm.io/docs/relations/eager-and-lazy-relations#relation-load-strategy)。

<!-- EN -->
* `loadEagerRelations` - controls whether eager relations (marked with `eager: true`) are automatically loaded. Defaults to `true`. See [Eager and Lazy Relations](https://typeorm.io/docs/relations/eager-and-lazy-relations#relation-load-strategy) for details.

<!-- ZH -->
* `loadEagerRelations` - 控制是否自动加载急加载关系（用 `eager: true` 标记的）。默认为 `true`。详见[急加载与懒加载关系](https://typeorm.io/docs/relations/eager-and-lazy-relations#relation-load-strategy)。

<!-- EN -->
* `where` - simple conditions by which entity should be queried.

<!-- ZH -->
* `where` - 查询实体所用的简单条件。

<!-- EN -->
```typescript
userRepository.find({
    where: {
        firstName: "Timber",
        lastName: "Saw",
    },
})
```

<!-- ZH -->
```typescript
userRepository.find({
    where: {
        firstName: "Timber",
        lastName: "Saw",
    },
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "user"
WHERE "firstName" = 'Timber' AND "lastName" = 'Saw'
```

<!-- ZH -->
```sql
SELECT * FROM "user"
WHERE "firstName" = 'Timber' AND "lastName" = 'Saw'
```

<!-- EN -->
Querying a column from an embedded entity should be done with respect to the hierarchy in which it was defined. Example:

<!-- ZH -->
查询嵌入式实体中的列时，应当遵循它被定义时的层级结构。示例：

<!-- EN -->
```typescript
userRepository.find({
    relations: {
        project: true,
    },
    where: {
        project: {
            name: "TypeORM",
            initials: "TORM",
        },
    },
})
```

<!-- ZH -->
```typescript
userRepository.find({
    relations: {
        project: true,
    },
    where: {
        project: {
            name: "TypeORM",
            initials: "TORM",
        },
    },
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "user"
LEFT JOIN "project" ON "project"."id" = "user"."projectId"
WHERE "project"."name" = 'TypeORM' AND "project"."initials" = 'TORM'
```

<!-- ZH -->
```sql
SELECT * FROM "user"
LEFT JOIN "project" ON "project"."id" = "user"."projectId"
WHERE "project"."name" = 'TypeORM' AND "project"."initials" = 'TORM'
```

<!-- EN -->
Querying with OR operator:

<!-- ZH -->
使用 OR 运算符查询：

<!-- EN -->
```typescript
userRepository.find({
    where: [
        { firstName: "Timber", lastName: "Saw" },
        { firstName: "Stan", lastName: "Lee" },
    ],
})
```

<!-- ZH -->
```typescript
userRepository.find({
    where: [
        { firstName: "Timber", lastName: "Saw" },
        { firstName: "Stan", lastName: "Lee" },
    ],
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "user" WHERE ("firstName" = 'Timber' AND "lastName" = 'Saw') OR ("firstName" = 'Stan' AND "lastName" = 'Lee')
```

<!-- ZH -->
```sql
SELECT * FROM "user" WHERE ("firstName" = 'Timber' AND "lastName" = 'Saw') OR ("firstName" = 'Stan' AND "lastName" = 'Lee')
```

<!-- EN -->
* `order` - selection order.

<!-- ZH -->
* `order` - 选取顺序。

<!-- EN -->
```typescript
userRepository.find({
    order: {
        name: "ASC",
        id: "DESC",
    },
})
```

<!-- ZH -->
```typescript
userRepository.find({
    order: {
        name: "ASC",
        id: "DESC",
    },
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "user"
ORDER BY "name" ASC, "id" DESC
```

<!-- ZH -->
```sql
SELECT * FROM "user"
ORDER BY "name" ASC, "id" DESC
```

<!-- EN -->
* `withDeleted` - include entities which have been soft deleted with `softDelete` or `softRemove`, e.g. have their `@DeleteDateColumn` column set. By default, soft deleted entities are not included.

<!-- ZH -->
* `withDeleted` - 包含那些已被 `softDelete` 或 `softRemove` 软删除的实体，例如它们的 `@DeleteDateColumn` 列已被设置。默认情况下，软删除的实体不会被包含。

<!-- EN -->
```typescript
userRepository.find({
    withDeleted: true,
})
```

<!-- ZH -->
```typescript
userRepository.find({
    withDeleted: true,
})
```

<!-- EN -->
`find*` methods which return multiple entities (`find`, `findBy`, `findAndCount`, `findAndCountBy`) also accept following options:

<!-- ZH -->
返回多个实体的 `find*` 方法（`find`、`findBy`、`findAndCount`、`findAndCountBy`）还接受以下选项：

<!-- EN -->
* `skip` - offset (paginated) from where entities should be taken.

<!-- ZH -->
* `skip` - 从哪个偏移量（分页）开始取实体。

<!-- EN -->
```typescript
userRepository.find({
    skip: 5,
})
```

<!-- ZH -->
```typescript
userRepository.find({
    skip: 5,
})
```

<!-- EN -->
```sql
SELECT * FROM "user"
OFFSET 5
```

<!-- ZH -->
```sql
SELECT * FROM "user"
OFFSET 5
```

<!-- EN -->
* `take` - limit (paginated) - max number of entities that should be taken.

<!-- ZH -->
* `take` - 限制（分页）——最多应取多少个实体。

<!-- EN -->
```typescript
userRepository.find({
    take: 10,
})
```

<!-- ZH -->
```typescript
userRepository.find({
    take: 10,
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "user"
LIMIT 10
```

<!-- ZH -->
```sql
SELECT * FROM "user"
LIMIT 10
```

<!-- EN -->
\*\* `skip` and `take` should be used together

<!-- ZH -->
\*\* `skip` 和 `take` 应当一起使用

<!-- EN -->
\*\* If you are using typeorm with MSSQL, and want to use `take` or `limit`, you need to use order as well or you will receive the following error: `'Invalid usage of the option NEXT in the FETCH statement.'`

<!-- ZH -->
\*\* 如果你把 typeorm 与 MSSQL 一起使用，并且想用 `take` 或 `limit`，你还需要同时使用 order，否则会收到如下错误：`'Invalid usage of the option NEXT in the FETCH statement.'`

<!-- EN -->
```typescript
userRepository.find({
    order: {
        columnName: "ASC",
    },
    skip: 0,
    take: 10,
})
```

<!-- ZH -->
```typescript
userRepository.find({
    order: {
        columnName: "ASC",
    },
    skip: 0,
    take: 10,
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "user"
ORDER BY "columnName" ASC
LIMIT 10 OFFSET 0
```

<!-- ZH -->
```sql
SELECT * FROM "user"
ORDER BY "columnName" ASC
LIMIT 10 OFFSET 0
```

<!-- EN -->
* `cache` - Enables or disables query result caching. See [caching](https://typeorm.io/docs/query-builder/caching) for more information and options.

<!-- ZH -->
* `cache` - 启用或禁用查询结果缓存。更多信息和选项见[缓存](https://typeorm.io/docs/query-builder/caching)。

<!-- EN -->
```typescript
userRepository.find({
    cache: true,
})
```

<!-- ZH -->
```typescript
userRepository.find({
    cache: true,
})
```

<!-- EN -->
* `lock` - Enables locking mechanism for query. Can be used only in `findOne` and `findOneBy` methods. `lock` is an object which can be defined as:

<!-- ZH -->
* `lock` - 为查询启用锁机制。只能在 `findOne` 和 `findOneBy` 方法中使用。`lock` 是一个对象，可以定义为：

<!-- EN -->
```ts
{ mode: "optimistic", version: number | Date }
```

<!-- ZH -->
```ts
{ mode: "optimistic", version: number | Date }
```

<!-- EN -->
or

<!-- ZH -->
或者

<!-- EN -->
```ts
{
    mode: "pessimistic_read" |
        "pessimistic_write" |
        "dirty_read" |
        "for_no_key_update" |
        "for_key_share",

    tables: string[],
    onLocked: "nowait" | "skip_locked"
}
```

<!-- ZH -->
```ts
{
    mode: "pessimistic_read" |
        "pessimistic_write" |
        "dirty_read" |
        "for_no_key_update" |
        "for_key_share",

    tables: string[],
    onLocked: "nowait" | "skip_locked"
}
```

<!-- EN -->
for example:

<!-- ZH -->
例如：

<!-- EN -->
```typescript
userRepository.findOne({
    where: {
        id: 1,
    },
    lock: { mode: "optimistic", version: 1 },
})
```

<!-- ZH -->
```typescript
userRepository.findOne({
    where: {
        id: 1,
    },
    lock: { mode: "optimistic", version: 1 },
})
```

<!-- EN -->
See [lock modes](https://typeorm.io/docs/query-builder/select-query-builder#lock-modes) for more information.

<!-- ZH -->
更多信息见[锁模式](https://typeorm.io/docs/query-builder/select-query-builder#lock-modes)。

<!-- EN -->
## Example

<!-- ZH -->
## 示例

<!-- EN -->
Complete example of find options:

<!-- ZH -->
查找选项的完整示例：

<!-- EN -->
```typescript
userRepository.find({
    select: {
        firstName: true,
        lastName: true,
    },
    relations: {
        profile: true,
        photos: true,
        videos: true,
    },
    where: {
        firstName: "Timber",
        lastName: "Saw",
        profile: {
            userName: "tshaw",
        },
    },
    order: {
        name: "ASC",
        id: "DESC",
    },
    lock: {
        mode: "pessimistic_write",
        onLocked: "nowait",
    },
    skip: 5,
    take: 10,
    cache: true,
})
```

<!-- ZH -->
```typescript
userRepository.find({
    select: {
        firstName: true,
        lastName: true,
    },
    relations: {
        profile: true,
        photos: true,
        videos: true,
    },
    where: {
        firstName: "Timber",
        lastName: "Saw",
        profile: {
            userName: "tshaw",
        },
    },
    order: {
        name: "ASC",
        id: "DESC",
    },
    lock: {
        mode: "pessimistic_write",
        onLocked: "nowait",
    },
    skip: 5,
    take: 10,
    cache: true,
})
```

<!-- EN -->
`count` APIs also accept these find options. When using `select`, you can request distinct counting:

<!-- ZH -->
`count` 系列 API 同样接受这些查找选项。使用 `select` 时，你可以要求去重计数：

<!-- EN -->
```typescript
const distinctCount = await userRepository.count({
    select: {
        firstName: true,
    },
})
```

<!-- ZH -->
```typescript
const distinctCount = await userRepository.count({
    select: {
        firstName: true,
    },
})
```

<!-- EN -->
When `select` includes relation fields, include matching entries in `relations` and the distinct count will use those selected relation columns.

<!-- ZH -->
当 `select` 包含关系字段时，请在 `relations` 中包含对应条目，去重计数会使用这些被选中的关系列。

<!-- EN -->
Find without arguments:

<!-- ZH -->
不带参数查找：

<!-- EN -->
```ts
userRepository.find()
```

<!-- ZH -->
```ts
userRepository.find()
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "user"
```

<!-- ZH -->
```sql
SELECT * FROM "user"
```

<!-- EN -->
## Advanced options

<!-- ZH -->
## 进阶选项

<!-- EN -->
TypeORM provides a lot of built-in operators that can be used to create more complex comparisons:

<!-- ZH -->
TypeORM 提供了大量内置运算符，可用于构造更复杂的比较：

<!-- EN -->
* `Not`

<!-- ZH -->
* `Not`

<!-- EN -->
```ts
import { Not } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: Not("About #1"),
})
```

<!-- ZH -->
```ts
import { Not } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: Not("About #1"),
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE "title" != 'About #1'
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE "title" != 'About #1'
```

<!-- EN -->
* `LessThan`

<!-- ZH -->
* `LessThan`

<!-- EN -->
```ts
import { LessThan } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    likes: LessThan(10),
})
```

<!-- ZH -->
```ts
import { LessThan } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    likes: LessThan(10),
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE "likes" < 10
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE "likes" < 10
```

<!-- EN -->
* `LessThanOrEqual`

<!-- ZH -->
* `LessThanOrEqual`

<!-- EN -->
```ts
import { LessThanOrEqual } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    likes: LessThanOrEqual(10),
})
```

<!-- ZH -->
```ts
import { LessThanOrEqual } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    likes: LessThanOrEqual(10),
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE "likes" <= 10
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE "likes" <= 10
```

<!-- EN -->
* `MoreThan`

<!-- ZH -->
* `MoreThan`

<!-- EN -->
```ts
import { MoreThan } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    likes: MoreThan(10),
})
```

<!-- ZH -->
```ts
import { MoreThan } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    likes: MoreThan(10),
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE "likes" > 10
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE "likes" > 10
```

<!-- EN -->
* `MoreThanOrEqual`

<!-- ZH -->
* `MoreThanOrEqual`

<!-- EN -->
```ts
import { MoreThanOrEqual } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    likes: MoreThanOrEqual(10),
})
```

<!-- ZH -->
```ts
import { MoreThanOrEqual } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    likes: MoreThanOrEqual(10),
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE "likes" >= 10
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE "likes" >= 10
```

<!-- EN -->
* `Equal`

<!-- ZH -->
* `Equal`

<!-- EN -->
```ts
import { Equal } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: Equal("About #2"),
})
```

<!-- ZH -->
```ts
import { Equal } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: Equal("About #2"),
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE "title" = 'About #2'
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE "title" = 'About #2'
```

<!-- EN -->
* `Like`

<!-- ZH -->
* `Like`

<!-- EN -->
```ts
import { Like } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: Like("%out #%"),
})
```

<!-- ZH -->
```ts
import { Like } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: Like("%out #%"),
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE "title" LIKE '%out #%'
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE "title" LIKE '%out #%'
```

<!-- EN -->
* `ILike`

<!-- ZH -->
* `ILike`

<!-- EN -->
```ts
import { ILike } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: ILike("%out #%"),
})
```

<!-- ZH -->
```ts
import { ILike } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: ILike("%out #%"),
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE "title" ILIKE '%out #%'
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE "title" ILIKE '%out #%'
```

<!-- EN -->
* `Between`

<!-- ZH -->
* `Between`

<!-- EN -->
```ts
import { Between } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    likes: Between(1, 10),
})
```

<!-- ZH -->
```ts
import { Between } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    likes: Between(1, 10),
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE "likes" BETWEEN 1 AND 10
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE "likes" BETWEEN 1 AND 10
```

<!-- EN -->
* `In`

<!-- ZH -->
* `In`

<!-- EN -->
```ts
import { In } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: In(["About #2", "About #3"]),
})
```

<!-- ZH -->
```ts
import { In } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: In(["About #2", "About #3"]),
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE "title" IN ('About #2','About #3')
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE "title" IN ('About #2','About #3')
```

<!-- EN -->
* `Any`

<!-- ZH -->
* `Any`

<!-- EN -->
```ts
import { Any } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: Any(["About #2", "About #3"]),
})
```

<!-- ZH -->
```ts
import { Any } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: Any(["About #2", "About #3"]),
})
```

<!-- EN -->
will execute following query (Postgres notation):

<!-- ZH -->
会执行如下查询（Postgres 记法）：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE "title" = ANY(['About #2','About #3'])
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE "title" = ANY(['About #2','About #3'])
```

<!-- EN -->
* `IsNull`

<!-- ZH -->
* `IsNull`

<!-- EN -->
```ts
import { IsNull } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: IsNull(),
})
```

<!-- ZH -->
```ts
import { IsNull } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: IsNull(),
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE "title" IS NULL
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE "title" IS NULL
```

<!-- EN -->
* `ArrayContains`

<!-- ZH -->
* `ArrayContains`

<!-- EN -->
```ts
import { ArrayContains } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    categories: ArrayContains(["TypeScript"]),
})
```

<!-- ZH -->
```ts
import { ArrayContains } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    categories: ArrayContains(["TypeScript"]),
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE "categories" @> '{TypeScript}'
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE "categories" @> '{TypeScript}'
```

<!-- EN -->
* `ArrayContainedBy`

<!-- ZH -->
* `ArrayContainedBy`

<!-- EN -->
```ts
import { ArrayContainedBy } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    categories: ArrayContainedBy(["TypeScript"]),
})
```

<!-- ZH -->
```ts
import { ArrayContainedBy } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    categories: ArrayContainedBy(["TypeScript"]),
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE "categories" <@ '{TypeScript}'
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE "categories" <@ '{TypeScript}'
```

<!-- EN -->
* `ArrayOverlap`

<!-- ZH -->
* `ArrayOverlap`

<!-- EN -->
```ts
import { ArrayOverlap } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    categories: ArrayOverlap(["TypeScript"]),
})
```

<!-- ZH -->
```ts
import { ArrayOverlap } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    categories: ArrayOverlap(["TypeScript"]),
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE "categories" && '{TypeScript}'
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE "categories" && '{TypeScript}'
```

<!-- EN -->
* `JsonContains` (PostgreSQL/CockroachDB only)

<!-- ZH -->
* `JsonContains`（仅 PostgreSQL/CockroachDB）

<!-- EN -->
```ts
import { JsonContains } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    metadata: JsonContains({ author: { name: "John" } }),
})
```

<!-- ZH -->
```ts
import { JsonContains } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    metadata: JsonContains({ author: { name: "John" } }),
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE "metadata" ::jsonb @> '{"author":{"name":"John"}}'
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE "metadata" ::jsonb @> '{"author":{"name":"John"}}'
```

<!-- EN -->
`JsonContains` also accepts arrays, so jsonb columns that store JSON arrays can be queried directly:

<!-- ZH -->
`JsonContains` 也接受数组，因此存放 JSON 数组的 jsonb 列可以被直接查询：

<!-- EN -->
```ts
import { JsonContains } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    tags: JsonContains([{ name: "TypeScript" }]),
})
```

<!-- ZH -->
```ts
import { JsonContains } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    tags: JsonContains([{ name: "TypeScript" }]),
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE "tags" ::jsonb @> '[{"name":"TypeScript"}]'
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE "tags" ::jsonb @> '[{"name":"TypeScript"}]'
```

<!-- EN -->
* `Raw`

<!-- ZH -->
* `Raw`

<!-- EN -->
```ts
import { Raw } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    likes: Raw("dislikes - 4"),
})
```

<!-- ZH -->
```ts
import { Raw } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    likes: Raw("dislikes - 4"),
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE "likes" = "dislikes" - 4
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE "likes" = "dislikes" - 4
```

<!-- EN -->
In the simplest case, a raw query is inserted immediately after the equal symbol. But you can also completely rewrite the comparison logic using the function.

<!-- ZH -->
在最简单的情况下，原生查询会被直接插到等号之后。不过你也可以用函数完全重写比较逻辑。

<!-- EN -->
```ts
import { Raw } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    currentDate: Raw((alias) => `${alias} > NOW()`),
})
```

<!-- ZH -->
```ts
import { Raw } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    currentDate: Raw((alias) => `${alias} > NOW()`),
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE "currentDate" > NOW()
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE "currentDate" > NOW()
```

<!-- EN -->
If you need to provide user input, you should not include the user input directly in your query as this may create a SQL injection vulnerability. Instead, you can use the second argument of the `Raw` function to provide a list of parameters to bind to the query.

<!-- ZH -->
如果你需要提供用户输入，不应把用户输入直接拼进查询中，因为这可能造成 SQL 注入漏洞。相反，你可以用 `Raw` 函数的第二个参数提供一组要绑定到查询上的参数。

<!-- EN -->
```ts
import { Raw } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    currentDate: Raw((alias) => `${alias} > :date`, { date: "2020-10-06" }),
})
```

<!-- ZH -->
```ts
import { Raw } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    currentDate: Raw((alias) => `${alias} > :date`, { date: "2020-10-06" }),
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE "currentDate" > '2020-10-06'
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE "currentDate" > '2020-10-06'
```

<!-- EN -->
If you need to provide user input that is an array, you can bind them as a list of values in the SQL statement by using the special expression syntax:

<!-- ZH -->
如果你需要提供数组形式的用户输入，可以用特殊的表达式语法把它们作为值列表绑定到 SQL 语句中：

<!-- EN -->
```ts
import { Raw } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: Raw((alias) => `${alias} IN (:...titles)`, {
        titles: [
            "Go To Statement Considered Harmful",
            "Structured Programming",
        ],
    }),
})
```

<!-- ZH -->
```ts
import { Raw } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: Raw((alias) => `${alias} IN (:...titles)`, {
        titles: [
            "Go To Statement Considered Harmful",
            "Structured Programming",
        ],
    }),
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE "title" IN ('Go To Statement Considered Harmful', 'Structured Programming')
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE "title" IN ('Go To Statement Considered Harmful', 'Structured Programming')
```

<!-- EN -->
## Combining Advanced Options

<!-- ZH -->
## 组合进阶选项

<!-- EN -->
Also you can combine these operators with below:

<!-- ZH -->
你还可以把这些运算符与下面的组合使用：

<!-- EN -->
* `Not`

<!-- ZH -->
* `Not`

<!-- EN -->
```ts
import { Not, MoreThan, Equal } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    likes: Not(MoreThan(10)),
    title: Not(Equal("About #2")),
})
```

<!-- ZH -->
```ts
import { Not, MoreThan, Equal } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    likes: Not(MoreThan(10)),
    title: Not(Equal("About #2")),
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE NOT("likes" > 10) AND NOT("title" = 'About #2')
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE NOT("likes" > 10) AND NOT("title" = 'About #2')
```

<!-- EN -->
* `Or`

<!-- ZH -->
* `Or`

<!-- EN -->
```ts
import { Or, Equal, ILike } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: Or(Equal("About #2"), ILike("About%")),
})
```

<!-- ZH -->
```ts
import { Or, Equal, ILike } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: Or(Equal("About #2"), ILike("About%")),
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE "title" = 'About #2' OR "title" ILIKE 'About%'
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE "title" = 'About #2' OR "title" ILIKE 'About%'
```

<!-- EN -->
* `And`

<!-- ZH -->
* `And`

<!-- EN -->
```ts
import { And, Not, Equal, ILike } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: And(Not(Equal("About #2")), ILike("%About%")),
})
```

<!-- ZH -->
```ts
import { And, Not, Equal, ILike } from "typeorm"

const loadedPosts = await dataSource.getRepository(Post).findBy({
    title: And(Not(Equal("About #2")), ILike("%About%")),
})
```

<!-- EN -->
will execute following query:

<!-- ZH -->
会执行如下查询：

<!-- EN -->
```sql
SELECT * FROM "post" WHERE NOT("title" = 'About #2') AND "title" ILIKE '%About%'
```

<!-- ZH -->
```sql
SELECT * FROM "post" WHERE NOT("title" = 'About #2') AND "title" ILIKE '%About%'
```
