Pradnya Sinalkar
Pradnya Sinalkar

Reputation: 1146

DynamoDB FilterExpression with multiple condition javascript

I have a table with partition key and sort key also 2 other columns. I am unable to get items using FilterExpression for multiple conditions with AND in DynamoDB using javaScript AWS SDK. Can anyone provide correct code to retrieve data with multiple conditions in FilterExpression? My code is as follows:

var params = {
    TableName: 'Department',
    KeyConditionExpression: '#company = :companyId'
    , ExpressionAttributeNames: {
        '#company': 'CompanyID',
        '#dType': 'DepartmentType',
        '#cTime': 'CreatedTime'
    }
    , ExpressionAttributeValues: {
        ':companyId': 'Test',
        ':deptType': dType,
        ':daysPrior': 1250456879634
    },FilterExpression: '#dType = :deptType AND #ts > :daysPrior' 
};

Upvotes: 27

Views: 55980

Answers (2)

Sunil B N
Sunil B N

Reputation: 4225

There is typo error in the format in your query(after CreatedTime) To keep it clean, use either double quotes or single quotes but not both. I have used double quotes, just the way aws sample codes are there.

var params = {
    TableName: "Department",
    KeyConditionExpression: "#company = :companyId", 
    ExpressionAttributeNames: {
        "#company": "CompanyID",
        "#dType": "DepartmentType",
        "#cTime": "CreatedTime" //here
    },
    ExpressionAttributeValues: {
        ":companyId": "Test",
        ":deptType": dType,
        ":daysPrior": 1250456879634
    },
    FilterExpression: "#dType = :deptType AND #cTime > :daysPrior" 
};

Upvotes: 46

Yuci
Yuci

Reputation: 30219

In case you need a bit more complex filter expressions to scan or query your DynamoDB table, you could use brackets, for example:

var params = {
    TableName: "Department",
    ...
    FilterExpression: "(not (#type <> :type and #company = :company)) or (#timestamp > :timestamp)",
    ...
};

Note: this example filter expression does not have a particular meaning.

Upvotes: 4

Related Questions