9

enter image description here

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 Answer 1

2

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);
   }
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the response Amrit pal,I tried your solution but it is throwing error "Unexpected key 'day' found in params.ExpressionAttributeValues[':dayVal']"
I've executed above code using your table data model (with column workingDays as list of object) and it's working for me, can you paste here the exception trace you are getting
I've been looking for a solution that works with Maps that have more than one property, but you only want to search for one property. [{"day": "Mon", "time": 4}, {"day": "Tue", "time": 4}] and then search for {"time": 4}
I'm trying to achieve the same result, filtering by an object property value, where the object is one fo many in an array. Unfortunately, this answer does not seem to work (My array is nested inside another object.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.