8

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)

        }
    })
RAHUL SRV
  • 262
  • 3
  • 24

1 Answers1

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);
   }
});
Amrit Pal Singh
  • 7,116
  • 7
  • 40
  • 50
  • Thanks for the response Amrit pal,I tried your solution but it is throwing error "Unexpected key 'day' found in params.ExpressionAttributeValues[':dayVal']" – RAHUL SRV Jan 04 '19 at 04:18
  • 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 – Amrit Pal Singh Jan 04 '19 at 06:28
  • 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} – ChazUK Mar 18 '21 at 22:19
  • 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.) – Mr Pablo Apr 06 '22 at 16:09