2017-02-27 137 views
0

我剛開始與亞馬遜dynamoDB,我要創造一個沒有SQL數據庫的結構是這樣,亞馬遜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 

現在,我想找回那些文本發佈具有代碼B or D的ID是什麼將是實現這一目標的最簡單方法?

+1

看起來您需要執行[Scan](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

正如上討論的意見,如果你保持tags屬性爲DynamoDB列表數據類型,您可以使用CONTAINSOR操作者檢查篩選具有標籤B or D的職位。

用於掃描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

因爲我想要,非常感謝你:) –