7

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

变量参数 = {

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

}

4

2 回答 2

6

根据文档:

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

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

您将需要分批执行查询/扫描,在您的情况下,第一批中有 100 个,Object.keys(profileIdObject).toString()第二批中有 19 个。然后合并结果。

于 2018-03-30T17:10:30.457 回答
2

根据 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 )
于 2020-07-03T13:29:08.717 回答