2016-10-27 105 views
3

檢查過AWS文檔,但沒有找到任何工作示例。如何在使用數組的FilterExpression中使用「IN」語句 - dynamodb

這是我嘗試

var params = { 
      TableName: "User", 
      IndexName:"a-b-index", 
      KeyConditionExpression: "Country = :country and #s = :status", 
      FilterExpression: "Id IN (:e)", 
      ExpressionAttributeValues: { 
       ":country ": "USA", 
       ":status": 1, 
       ":e": "1" 

      }, 
      ExpressionAttributeNames: {"#s": "Status"} 
      }; 

      //get users 
      dynamodb.query(params, function (err, data) { 
      if (err) 
       //error 
      else { 
       //success 

      } 
      }); 

了記錄,但它是取它具有ID 1紀錄,但我想用數組這樣

var params = { 
     TableName: "User", 
     IndexName:"a-b-index", 
     KeyConditionExpression: "Country = :country and #s = :status", 
     FilterExpression: "Id IN (:e)", 
     ExpressionAttributeValues: { 
      ":country ": "USA", 
      ":status": 1, 
      ":e": ["1","2","3"] 

     }, 
     ExpressionAttributeNames: {"#s": "Status"} 
     }; 

     //get users 
     dynamodb.query(params, function (err, data) { 
     if (err) 
      //error 
     else { 
      //success 

     } 
     }); 

怎樣才能使上面的代碼爲working.want獲取記錄。語法沒有錯誤糾正和查詢運行,但我沒有得到記錄

+0

你是怎麼測試它? –

+0

@NiroshanRanapathi你是什麼意思? –

+0

@NiroshanRanapathi我正在使用無服務器framework.checking本地 –

回答

7

請參閱本answer

摘要: -

對於「IN」的條款固定數量的值: -

var params = { 
    TableName : "Users", 
    FilterExpression : "username IN (:user1, :user2)", 
    ExpressionAttributeValues : { 
     ":user1" : "john", 
     ":user2" : "mike" 
    } 
}; 

對於陣列和動態形成FilterExpression多種元素: -

var titleValues = ["The Big New Movie 2012", "The Big New Movie"]; 
var titleObject = {}; 
var index = 0; 
titleValues.forEach(function(value) { 
    index++; 
    var titleKey = ":titlevalue"+index; 
    titleObject[titleKey.toString()] = value; 
}); 

var params = { 
    TableName : "Movies", 
    FilterExpression : "title IN ("+Object.keys(titleObject).toString()+ ")", 
    ExpressionAttributeValues : titleObject 
}; 
+0

感謝您的答案,將嘗試它,dynamodb不支持連接,我想基於其他表記錄獲取記錄,所以我想在操作符中使用,因爲沒有任何其他選項,但你在性能方面,對於1000位用戶來說,這不是最好的方法嗎? –

+0

在這種情況下,您需要將其導出或流式傳輸到EMR或其他東西,並檢索所需的信息。大多數NOSQL數據庫不支持連接。其他選項是查看數據模型,如果大小小於4KB,則將所有必需的數據存儲在一個表中。請注意,SO中的大部分答案都不是專門爲該要求而編寫的。有時他們只是解決問題的指針。 – notionquest

4

notionquest的答案是正確的,但我不能用我的其他值ExpressionAttributeValues:country:status所以這裏被修改的答案,使其工作按我的要求

var AttributeValuesObject = {}; 

    AttributeValuesObject[':country '] = "USA"; 
    AttributeValuesObject[':status'] = 1; 

    var titleValues = ["1", "2"]; 
    var titleObject = {}; 

    var index = 0; 

    titleValues.forEach(function(value) { 
    index++; 
    var titleKey = ":titleValue"+index; 
    AttributeValuesObject[titleKey.toString()] = value; 
    titleObject[titleKey.toString()] = value; 
    }); 

    var params = { 
    TableName: "User", 
    IndexName:"a-b-index", 
    KeyConditionExpression: "Country = :country and #s = :status", 
    FilterExpression: "Id IN ("+Object.keys(titleObject).toString()+ ")", 
    ExpressionAttributeValues: AttributeValuesObject, 
    ExpressionAttributeNames: {"#s": "Status"} 
    }; 

    //get users 
    dynamodb.query(params, function (err, data) { 
    if (err) 
     //error 
    else { 
     //success 

    } 
    }); 
+0

如果這個概念不是專門爲您的用例編寫的,那麼您能否接受答案? – notionquest