# 使用实体服务 API 进行筛选

> Source: https://strapi.nodejs.cn/cms/api/entity-service/filter

🌐 Filtering with the Entity Service API

使用 `findMany()` 中的 `filters` 参数，通过逻辑运算符（`$and`、`$or`、`$not`）和属性运算符（`$eq`、`$contains`、`$gt`、`$between` 等）过滤实体服务 API 查询结果。

🌐 Filter Entity Service API query results using logical operators (`$and`, `$or`, `$not`) and attribute operators (`$eq`, `$contains`, `$gt`, `$between`, etc.) with the `filters` parameter in `findMany()`.

:::caution

在 Strapi v5 中，实体服务 API 已被弃用。请考虑改用 [文档服务 API](/cms/api/document-service)。

🌐 The Entity Service API is deprecated in Strapi v5. Please consider using the [Document Service API](/cms/api/document-service) instead.

:::

[实体服务 API](/cms/api/entity-service) 提供了使用其 [findMany()](/cms/api/entity-service/crud#findmany) 方法筛选结果的功能。

🌐 The [Entity Service API](/cms/api/entity-service) offers the ability to filter results found with its [findMany()](/cms/api/entity-service/crud#findmany) method.

结果通过 `filters` 参数进行过滤，该参数接受 [逻辑运算符](#logical-operators) 和 [属性运算符](#attribute-operators)。每个运算符都应以 `$` 为前缀。

🌐 Results are filtered with the `filters` parameter that accepts [logical operators](#logical-operators) and [attribute operators](#attribute-operators). Every operator should be prefixed with `$`.

:::strapi Deep filtering with the various APIs

有关如何使用各种 API 进行深度过滤的示例，请参阅 [this blog article](https://strapi.io/blog/deep-filtering-alpha-26)。

:::

## 逻辑运算符 {#logical-operators}

🌐 Logical operators

### `$and`

所有嵌套条件必须是 `true`。

🌐 All nested conditions must be `true`.

**示例**

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    $and: [
      {
        title: 'Hello World',
      },
      {
        createdAt: { $gt: '2021-11-17T14:28:25.843Z' },
      },
    ],
  },
});
```

`$and` 在传递带有嵌套条件的对象时将被隐式使用：

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    title: 'Hello World',
    createdAt: { $gt: '2021-11-17T14:28:25.843Z' },
  },
});
```

### `$or`

一个或多个嵌套条件必须是 `true`。

🌐 One or many nested conditions must be `true`.

**示例**

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    $or: [
      {
        title: 'Hello World',
      },
      {
        createdAt: { $gt: '2021-11-17T14:28:25.843Z' },
      },
    ],
  },
});
```

### `$not`

否定嵌套条件。

🌐 Negates the nested conditions.

**示例**

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    $not: {
      title: 'Hello World',
    },
  },
});
```

:::note

`$not` 可以用作：

- 一个逻辑运算符（例如在 `filters: { $not: { // conditions… }}` 中）
- [属性操作符](#not-1)（例如在 `filters: { attribute-name: $not: { … } }` 中）。

:::

:::tip

`$and`、`$or` 和 `$not` 操作符可以嵌套在另一个 `$and`、`$or` 或 `$not` 操作符中。

:::

## 属性运算符 {#attribute-operators}

🌐 Attribute Operators

:::caution

根据数据库的实现，使用这些运算符可能会给出不同的结果，因为比较是由数据库而不是 Strapi 处理的。

🌐 Using these operators may give different results depending on the database's implementation, as the comparison is handled by the database and not by Strapi.

:::

### `$not`

否定嵌套条件。

🌐 Negates the nested condition(s).

**示例**

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    title: {
      $not: {
        $contains: 'Hello World',
      },
    },
  },
});
```

### `$eq`

属性等于输入值。

🌐 Attribute equals input value.

**示例**

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    title: {
      $eq: 'Hello World',
    },
  },
});
```

`$eq`可以省略：

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    title: 'Hello World',
  },
});
```

### `$eqi`

属性等于输入值（不区分大小写）。

🌐 Attribute equals input value (case-insensitive).

**示例**

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    title: {
      $eqi: 'HELLO World',
    },
  },
});
```

### `$ne`

属性不等于输入值。

🌐 Attribute does not equal input value.

**示例**

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    title: {
      $ne: 'ABCD',
    },
  },
});
```

### `$nei`

属性不等于输入值（不区分大小写）。

🌐 Attribute does not equal input value (case-insensitive).

**示例**

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    title: {
      $nei: 'abcd',
    },
  },
});
```

### `$in`

属性包含在输入列表中。

🌐 Attribute is contained in the input list.

**示例**

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    title: {
      $in: ['Hello', 'Hola', 'Bonjour'],
    },
  },
});
```

在传递一个值数组时可以省略 `$in`：

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    title: ['Hello', 'Hola', 'Bonjour'],
  },
});
```

### `$notIn`

输入列表中不包含属性。

🌐 Attribute is not contained in the input list.

**示例**

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    title: {
      $notIn: ['Hello', 'Hola', 'Bonjour'],
    },
  },
});
```

### `$lt`

属性小于输入值。

🌐 Attribute is less than the input value.

**示例**

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    rating: {
      $lt: 10,
    },
  },
});
```

### `$lte`

属性小于或等于输入值。

🌐 Attribute is less than or equal to the input value.

**示例**

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    rating: {
      $lte: 10,
    },
  },
});
```

### `$gt`

属性大于输入值。

🌐 Attribute is greater than the input value.

**示例**

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    rating: {
      $gt: 5,
    },
  },
});
```

### `$gte`

属性大于或等于输入值。

🌐 Attribute is greater than or equal to the input value.

**示例**

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    rating: {
      $gte: 5,
    },
  },
});
```

### `$between`

属性介于两个输入值之间，包括边界（例如，`$between[1, 3]` 也会返回 `1` 和 `3`）。

🌐 Attribute is between the 2 input values, boundaries included (e.g., `$between[1, 3]` will also return `1` and `3`).

**示例**

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    rating: {
      $between: [1, 20],
    },
  },
});
```

### `$contains`

属性包含输入值（区分大小写）。

🌐 Attribute contains the input value (case-sensitive).

**示例**

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    title: {
      $contains: 'Hello',
    },
  },
});
```

### `$notContains`

属性不包含输入值（区分大小写）。

🌐 Attribute does not contain the input value (case-sensitive).

**示例**

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    title: {
      $notContains: 'Hello',
    },
  },
});
```

### `$containsi`

属性包含输入值。`$containsi`不区分大小写，而[$contains](#contains)区分大小写。

🌐 Attribute contains the input value. `$containsi` is not case-sensitive, while [$contains](#contains) is.

**示例**

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    title: {
      $containsi: 'hello',
    },
  },
});
```

### `$notContainsi`

属性不包含输入值。`$notContainsi` 不区分大小写，而 [$notContains](#notcontains) 区分大小写。

🌐 Attribute does not contain the input value. `$notContainsi` is not case-sensitive, while [$notContains](#notcontains) is.

**示例**

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    title: {
      $notContainsi: 'hello',
    },
  },
});
```

### `$startsWith`

属性以输入值开始。

🌐 Attribute starts with input value.

**示例**

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    title: {
      $startsWith: 'ABCD',
    },
  },
});
```

### `$endsWith`

属性以输入值结尾。

🌐 Attribute ends with input value.

**示例**

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    title: {
      $endsWith: 'ABCD',
    },
  },
});
```

### `$null`

属性是 `null`。

🌐 Attribute is `null`.

**示例**

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    title: {
      $null: true,
    },
  },
});
```

### `$notNull`

属性不是 `null`。

🌐 Attribute is not `null`.

**示例**

```js
const entries = await strapi.entityService.findMany('api::article.article', {
  filters: {
    title: {
      $notNull: true,
    },
  },
});
```
