How to Query a Array of objects in DynamoDB using FilterExpression in scan operation

爷,独闯天下 提交于 2021-02-07 06:38:12

问题


How to Query array of objects(workingDays) key containing only "Tue" in dynamoDb with Scan Operation,I have queried using filter Expression but i am getting no results.

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:


ExpressionAttributeValues in your query contains String ["S"] as 'key' for value 'Tue', where as in your table, 'workingDays' is a list of map object containing value for day keys.

Try below code:

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);
   }
});


来源:https://stackoverflow.com/questions/54002458/how-to-query-a-array-of-objects-in-dynamodb-using-filterexpression-in-scan-opera

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!