2017-02-27 4 views
0

さて、アマゾンでDynamoDBの「OR」条件

-posts 
    -postId1 
     -tags 
      1:A 
      2:B 
      3:C 
     -text:Hello 

    -postId2 
     -tags 
      1:B 
      2:D 
     -text:How are you? 

    -postId3 
     -tags 
      1:A 
      2:C 
     -text:Hello World 

を私はちょうどアマゾンDynamoDBので始まりだし、私はこのような無のSQL DBの構造を作成する必要があり、私はそれらのテキストを取得したいですタグB or Dを持っている投稿IDはこれを達成する最も簡単な方法でしょうか?コメントに述べたように、あなたがDynamoDBのリストデータ型としてtags属性を維持する場合

+1

あなたは[スキャン](http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html)操作を実行する必要がありますように見えます。 –

+0

タグリストの複数の項目でOR条件を使用できますか? –

+1

これを行うこともできます。または、フィルタ式で[IN](http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.SpecifyingConditions.html#ConditionExpressionReference)キーワードを使用して比較することもできます列挙された値のリスト(あなたの 'tags 'が** Map **か** List **かどうかは確かではありません) –

答えて

1

、あなたはタグB or Dを持っている投稿をフィルタリングするためにチェックするCONTAINSOR演算子を使用することができます。スキャンAPIのための

サンプルのparams: -

var params = { 
    TableName: "post", 
    FilterExpression: "contains (tags, :tag1) OR contains (tags, :tag2)", 
    ExpressionAttributeValues: { 
     ":tag1": 'B', 
     ":tag2": 'D' 
    } 
}; 

全コード: -

以下のコードは、地元DynamoDBのを使用しています。

var AWS = require("aws-sdk"); 
var creds = new AWS.Credentials('akid', 'secret', 'session'); 

AWS.config.update({ 
    region: "us-west-2", 
    endpoint: "http://localhost:8000", 
    credentials: creds 
}); 

var docClient = new AWS.DynamoDB.DocumentClient(); 

var params = { 
    TableName: "post", 
    FilterExpression: "contains (tags, :tag1) OR contains (tags, :tag2)", 
    ExpressionAttributeValues: { 
     ":tag1": 'B', 
     ":tag2": 'D' 
    } 
}; 

console.log("Scanning Post table."); 
docClient.scan(params, onScan); 

function onScan(err, data) { 
    if (err) { 
     console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2)); 
    } else { 
     console.log("Scan succeeded."); 
     data.Items.forEach(function (printItem) { 
      console.log("Item :", JSON.stringify(printItem)); 
     }); 

     if (typeof data.LastEvaluatedKey != "undefined") { 
      console.log("Scanning for more..."); 
      params.ExclusiveStartKey = data.LastEvaluatedKey; 
      docClient.scan(params, onScan); 
     } 
    } 
} 
+0

私が欲しかったように働いた、ありがとうございました:) –

関連する問題