IN运算符提供了太多的操作数;操作数:119动力

时间:2018-02-13 09:09:07

标签: amazon-dynamodb dynamo-local

尝试在dynamodb中使用IN操作但是会出现以下错误。任何人都可以帮我替代解决方案吗?

var params = {

TableName : "table_name",
FilterExpression : "id IN ("+Object.keys(profileIdObject).toString()+ ")",
ExpressionAttributeValues : profileIdObject

};

错误:: {

  "message": "Invalid FilterExpression: The IN operator is provided with too many operands; number of operands: 119",
  "code": "ValidationException",
  "time": "2018-02-13T08:48:02.597Z",
  "statusCode": 400,
  "retryable": false,
  "retryDelay": 25.08276239472692

}

2 个答案:

答案 0 :(得分:5)

根据文件:

  

IN比较器的最大操作数数为100

在此处找到:https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Limits.html#limits-expression-parameters

您将需要多批次执行查询/扫描,例如,第一批中有100个Object.keys(profileIdObject).toString(),第二批中有19个。然后合并结果。

答案 1 :(得分:1)

根据dynamodb文档,IN比较器的最大操作数为100

因此您可以分为许多操作,例如:

FilterExpression : "id IN (1,2,3, ....) OR id IN (101,102,103,...) ..."

使用此功能:

let getFilterExp = function (x) {
    let arr = []
    let currentIndex = 0
    let counter = 0
    let max = 99

    arr[currentIndex] = {}

    for (let y in x) {
        if (counter < max) {
            arr[currentIndex][y] = x[y]
            counter++
        }
        else {
            currentIndex++
            arr[currentIndex] = {}
            arr[currentIndex][y] = x[y]
            counter = 0
        }
    }

    let exp = ''
    for (let i = 0; i < arr.length; i++) {
        if (i == 0) {
            exp += "id IN (" + Object.keys(arr[i]).toString() + ")"
        }
        else {
            exp += " OR id IN (" + Object.keys(arr[i]).toString() + ") "
        }
    }

    return exp
}

其中x是您所用的profileIdObject

let filterExp = getFilterExp(profileIdObject )