

# DynamoDB の条件式とフィルター式、演算子、関数
<a name="Expressions.OperatorsAndFunctions"></a>

Amazon 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>

以下の構文の概要で、*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* がリスト内の任意の値と等しい場合、True。例では、*b*、*c*、*d* のいずれかと等しい場合。リストには、コンマで区切って最大 100 個の値を指定できます。

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

以下の関数を使用して、ある属性が項目に存在するか判定したり、属性の値を評価したりします。これらの関数名では大文字と小文字が区別されます。入れ子の属性では、完全ドキュメントパスを指定する必要があります。


****  

| 関数 | 説明 | 
| --- | --- | 
|  `attribute_exists (path)`  | 項目に、`path` で指定した属性が含まれる場合、true。 例: `Product` テーブルの項目に側面図があるかどうかを確認します。 [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html)  | 
|  `attribute_not_exists (path)`  | `path` で指定した属性が項目に存在しない場合、true。 例: 項目に `Manufacturer` 属性があるかどうかを確認します。 [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/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/ja_jp/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html) `type` パラメータには、式の属性値を使用する必要があります。 例: `QuantityOnHand` 属性がリスト型のものであるかどうかを確認します。この例では、`:v_sub` は文字列 `L` のプレースホルダーです。 [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html) `type` パラメータには、式の属性値を使用する必要があります。  | 
|  `begins_with (path, substr)`  |  `path` で指定された属性が特定のサブ文字列から始まる場合、true。 例: 正面図 URL の最初の数文字が `http://` かどうかを確認します。 [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html) 式の属性値 `:v_sub` は、`http://` のプレースホルダーです。  | 
|  `contains (path, operand)`  | `path` で指定された属性が次の属性である場合、true。 [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html) `path` で指定された属性が `String` である場合、`operand` は `String` である必要があります。`path` で指定された属性が `Set` の場合、`operand` は一連の要素タイプである必要があります。 パスとオペランドは区別する必要があります。つまり、`contains (a, a)` がエラーを返します。 例: `Brand` 属性にサブ文字列 `Company` が含まれているかどうかを確認します。 [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html) 式の属性値 `:v_sub` は、`Company` のプレースホルダーです。 例: 製品が赤で入手可能かどうかを確認します。 [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html) 式の属性値 `:v_sub` は、`Red` のプレースホルダーです。 | 
|  `size (path)`  | 属性のサイズを表す数値を返します。以下は、`size` で使用できる有効なデータ型です。  属性は `String` 型で、`size` は文字列の長さを返します。 例: 文字列 `Brand` が 20 文字以下であるかどうかを確認します。式の属性値 `:v_sub` は、`20` のプレースホルダーです。 [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html)  属性が `Binary` バイナリ型の場合、`size` は属性値のバイト数を返します。 例: `ProductCatalog` 項目に `VideoClip` という名前のバイナリ属性があるとします。この属性には使用中の製品の短いビデオが含まれます。次の式は、`VideoClip` が 64,000 バイトを超えるかどうかを確認します。式の属性値 `:v_sub` は、`64000` のプレースホルダーです。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html)  属性が `Set` データ型の場合、`size` は設定の要素数を返します。 例: 製品が複数の色で入手可能かどうかを確認します。式の属性値 `:v_sub` は、`1` のプレースホルダーです。 [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html)  属性が `List` 型または `Map` のものである場合、`size` は子要素の数を返します。 例: `OneStar` のレビューの数が特定のしきい値を超えたかどうかを確認します。式の属性値 `:v_sub` は、`3` のプレースホルダーです。 [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/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 の場合は Falsee。

以下は、オペレーションの 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`