

# DynamoDB 中的条件表达式和筛选表达式、运算符及函数
<a name="Expressions.OperatorsAndFunctions"></a>

要操作 DynamoDB 表中的数据，请使用 `PutItem`、`UpdateItem` 和 `DeleteItem` 操作。对于这些数据处理操作，您可指定条件表达式 来确定应修改的项目。如果条件表达式的计算结果为 true，则操作成功。否则，该操作将失败。

本节介绍用于在 Amazon DynamoDB 中编写筛选表达式和条件表达式的内置函数和关键字。有关 DynamoDB 的函数和编程的更多详细信息，请参阅[使用 DynamoDB 和 AWS SDK 编程](Programming.md)和 [DynamoDB API 参考](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/)。

**Topics**
+ [筛选条件和条件表达式的语法](#Expressions.OperatorsAndFunctions.Syntax)
+ [进行比较](#Expressions.OperatorsAndFunctions.Comparators)
+ [函数](#Expressions.OperatorsAndFunctions.Functions)
+ [逻辑评估](#Expressions.OperatorsAndFunctions.LogicalEvaluations)
+ [圆括号](#Expressions.OperatorsAndFunctions.Parentheses)
+ [条件的优先顺序](#Expressions.OperatorsAndFunctions.Precedence)

## 筛选条件和条件表达式的语法
<a name="Expressions.OperatorsAndFunctions.Syntax"></a>

在以下语法摘要中，{{操作数}} 可以为下列对象：
+ 顶级属性名称，例如 `Id`、`Title`、`Description` 或 `ProductCategory`
+ 引用嵌套属性的文档路径

```
condition-expression ::=
      {{operand}} comparator {{operand}}
    | {{operand}} BETWEEN {{operand}} AND {{operand}}
    | {{operand}} IN ( {{operand}} (',' {{operand}} (, ...) ))
    | function
    | {{condition}} AND {{condition}}
    | {{condition}} OR {{condition}}
    | NOT {{condition}}
    | ( {{condition}} )

comparator ::=
    =
    | <>
    | <
    | <=
    | >
    | >=

function ::=
    attribute_exists ({{path}})
    | attribute_not_exists ({{path}})
    | attribute_type ({{path}}, {{type}})
    | begins_with ({{path}}, {{substr}})
    | contains ({{path}}, {{operand}})
    | size ({{path}})
```

## 进行比较
<a name="Expressions.OperatorsAndFunctions.Comparators"></a>

使用以下比较器将操作数与单个值进行比较：
+ `{{a}} = {{b}}` – 如果 {{a}} 等于 {{b}}，则为 True。
+ `{{a}} <> {{b}}` – 如果 {{a}} 不等于 {{b}}，则为 True。
+ `{{a}} < {{b}}` – 如果 {{a}} 小于 {{b}}，则为 True。
+ `{{a}} <= {{b}}` – 如果 {{a}} 小于等于 {{b}}，则为 True。
+ `{{a}} > {{b}}` – 如果 {{a}} 大于 {{b}}，则为 True。
+ `{{a}} >= {{b}}` – 如果 {{a}} 大于等于 {{b}}，则为 True。

使用 `BETWEEN` 和 `IN` 关键字来将操作数与值范围或值的枚举值列表进行比较：
+ `{{a}} BETWEEN {{b}} AND {{c}}` – 如果 {{a}} 大于或等于 {{b}}，且小于或等于 {{c}}，则为 True。
+ `{{a}} IN ({{b}}, {{c}}, {{d}}) ` – 如果 {{a}} 等于列表中的任何值 — 例如 {{b}}、{{c}} 或 {{d}}，则为 True。列表最多可以包含 100 个值，以逗号分隔。

## 函数
<a name="Expressions.OperatorsAndFunctions.Functions"></a>

使用以下函数确定项目中是否存在某个属性，或者对属性求值。这些函数名称区分大小写。对于嵌套属性，您必须提供其完整文档路径。


****  

| 函数 | 说明 | 
| --- | --- | 
| `attribute_exists ({{path}})` | 如果项目包含 `path` 指定的属性，则为 true。<br />示例：检查 `Product` 表中的项目是否具有侧视图图片。[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html) | 
| `attribute_not_exists ({{path}})` | 如果项目中不存在由 `path` 指定的属性，则为 true。<br />示例：检查项目是否具有 `Manufacturer` 属性。[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html) | 
| `attribute_type ({{path}}, {{type}})` | 如果指定路径中的属性为特定数据类型，则为 true。`type` 参数必须是下列类型之一：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html)<br />您必须使用 `type` 参数的表达式属性值。<br />示例：检查 `QuantityOnHand` 属性是否为列表类型。在本示例中，`:v_sub` 为字符串 `L` 的占位符。[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html)<br />您必须使用 `type` 参数的表达式属性值。 | 
| `begins_with ({{path}}, {{substr}})` | 如果 `path` 指定的属性以特定子字符串开头，则为 true。<br />示例：检查前视图图片 URL 的前几个字符是否为 `http://`。[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html)<br />表达式属性值 `:v_sub` 是 `http://` 的占位符。 | 
| `contains ({{path}}, {{operand}})` | 如果 `path` 指定的属性为以下之一，则为 true：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html)<br />如果由 `path` 指定的属性为 `String`，则 `operand` 必须为 `String`。如果指定的属性 `path` 是一个 `Set`，`operand` 必须是集合的元素类型。<br />路径和操作数必须不同。也就是说，`contains (a, a)` 返回错误。<br />示例：检查 `Brand` 属性是否包含子字符串 `Company`。[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html)<br />表达式属性值 `:v_sub` 是 `Company` 的占位符。<br />示例：检查产品是否有红色。[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html)<br />表达式属性值 `:v_sub` 是 `Red` 的占位符。 | 
| `size ({{path}})` | 返回一个代表属性大小的数字。以下是与 `size` 结合使用的有效数据类型。<br /><br />如果属性类型为 `String`，则 `size` 将返回字符串的长度。<br />示例：检查字符串 `Brand` 是否少于等于 20 个字符。表达式属性值 `:v_sub` 是 `20` 的占位符。[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html)<br /><br />如果属性类型为 `Binary`，则 `size` 将返回属性值中的字节数。<br />示例：假设 `ProductCatalog` 项目有一个名为 `VideoClip` 的二进制属性，该属性包含使用中的产品的简短视频。以下表达式将检查 `VideoClip` 是否超过 64000 个字节。表达式属性值 `:v_sub` 是 `64000` 的占位符。[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html)<br /><br />如果属性是一个 `Set` 数据类型，则 `size` 将返回集合中的元素数。<br />示例：检查产品是否有多种颜色。表达式属性值 `:v_sub` 是 `1` 的占位符。[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html)<br /><br />如果属性类型为 `List` 或 `Map`，则 `size` 将返回子元素数。<br />示例：检查 `OneStar` 评论的数量是否超过了特定阈值。表达式属性值 `:v_sub` 是 `3` 的占位符。[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html) | 

## 逻辑评估
<a name="Expressions.OperatorsAndFunctions.LogicalEvaluations"></a>

使用 `AND`、`OR` 和 `NOT` 关键字执行逻辑评估。在以下列表中，{{a}} 和 {{b}} 代表要评估的条件。
+ `{{a}} AND {{b}}` – 如果 {{a}} 和 {{b}} 均为 true，则为 True。
+ `{{a}} OR {{b}}` – 如果 {{a}} 和/或 {{b}} 为 true，则为 True。
+ `NOT {{a}}` – 如果 {{a}} 为 false，则为 True。如果 {{a}} 为 true，则为 False。

以下是操作中 AND 的代码示例。

`dynamodb-local (*)> select * from exprtest where a > 3 and a < 5;`

## 圆括号
<a name="Expressions.OperatorsAndFunctions.Parentheses"></a>

使用圆括号更改逻辑评估的优先顺序。例如，假设条件 {{a}} 和 {{b}} 为 true，而条件 {{c}} 为 false。以下表达式的计算结果为 True：
+ `{{a}} OR {{b}} AND {{c}}`

但是，如果将一个条件括在圆括号中，则会先对该条件求值。例如，以下表达式的计算结算为 False：
+  `({{a}} OR {{b}}) AND {{c}}`

**注意**  
您可以在表达式中嵌套圆括号。最里面的部分最先评估。

以下是逻辑评估中带有括号的代码示例。

`dynamodb-local (*)> select * from exprtest where attribute_type(b, string) or ( a = 5 and c = “coffee”);`

## 条件的优先顺序
<a name="Expressions.OperatorsAndFunctions.Precedence"></a>

 DynamoDB 使用以下优先顺序规则从左向右评估条件：
+ `= <> < <= > >=`
+ `IN`
+ `BETWEEN`
+ `attribute_exists attribute_not_exists begins_with contains`
+ 圆括号
+ `NOT`
+ `AND`
+ `OR`