如何在扫描操作中使用FilterExpression在DynamoDB中查询对象数组

时间:2019-01-02 07:01:33

标签: aws-lambda amazon-dynamodb dynamodb-queries

enter image description here

如何通过扫描操作在dynamoDb中查询仅包含“ Tue”的对象(workingDays)键数组,我已使用过滤器表达式查询,但没有任何结果。

var queryData = {
        TableName: tableName,
        FilterExpression: "contains (workingDays, :dayVal)",
        ExpressionAttributeValues: {
            ":dayVal": {
                S:"Tue"
            }
        }
    };

    console.log("getParams ==>", queryData)
    dynamodb.scan(queryData, function (err, details) {
        if (err) {
            console.log(err, err.stack); // an error occurred
            callback(err, null)
        }
        else{
           callback(null, details)

        }
    })

1 个答案:

答案 0 :(得分:0)

您查询中的

ExpressionAttributeValues 包含字符串[“ S”]作为值“ Tue”的“键”,如表中的“ workingDays” 所示地图对象包含日期键的值。

尝试以下代码:

var queryData = {
    TableName: tableName,
    ExpressionAttributeNames: {
         "#workingDays": "workingDays",
    },
    FilterExpression: "contains (#workingDays, :dayVal)",
    ExpressionAttributeValues: {
      ":dayVal": {
          "day":"Tue"
      }
   }
};

console.log("getParams ==>", queryData)

docClient.scan(queryData, function (err, details) {
    if (err) {
        console.log(err, err.stack); // an error occurred
       // callback(err, null)
    }
    else{
       // callback(null, details)
       console.log(details);
   }
});