

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# DynamoDB 中的條件表達式、篩選條件表達式、運算子和函數。
<a name="Expressions.OperatorsAndFunctions"></a>

若要操控 DynamoDB 資料表中的資料，您可以使用 `PutItem`、`UpdateItem` 和 `DeleteItem` 操作。針對這些資料操控操作，您可以指定條件表達式來判斷應該修改的項目。如果條件表達式評估為 true，則操作會成功。否則，操作會失敗。

本節說明在 Amazon DynamoDB 中撰寫篩選條件表達式和條件表達式所使用的內建函數與關鍵字。如需有關 DynamoDB 的函數和進行程式設計的詳細資訊，請參閱 [使用 DynamoDB 和 AWS SDKs程式設計](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>

在以下語法摘要中，{{operand}} 可以是以下項目：
+ 最上層屬性名稱，例如 `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>

使用下列函數可判斷項目中是否具有某屬性，或評估某屬性的值。這些函數名稱區分大小寫。針對巢狀屬性，您必須提供其完整文件路徑。


****  

| 函式 | Description | 
| --- | --- | 
| `attribute_exists ({{path}})` | 如果項目包含 `path` 指定的屬性，則為 true。<br />範例：檢查 `Product` 資料表中的項目是否具有側視圖。[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html)<br /><br />如果屬性的類型為 `Binary`，`size` 會傳回屬性值中的位元組數目。<br />範例：假設 `ProductCatalog` 項目具有名為 `VideoClip` 的二進制屬性，其中包含使用中產品的短片。以下表達式會檢查 `VideoClip` 是否超過 64,000 個位元組。表達式屬性值 `:v_sub` 是 `64000` 的預留位置。[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/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_tw/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_tw/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`