Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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' 
};
like image 655
Pradnya Sinalkar Avatar asked Jul 20 '17 10:07

Pradnya Sinalkar


People also ask

What is Filterexpression in DynamoDB?

A filter expression determines which items within the Query results should be returned to you. All of the other results are discarded. A filter expression is applied after a Query finishes, but before the results are returned.

Which is faster scan or Query in DynamoDB?

For faster response times, design your tables and indexes so that your applications can use Query instead of Scan . (For tables, you can also consider using the GetItem and BatchGetItem APIs.)

What is the difference between Query and scan operations in DynamoDB?

DynamoDB offers two ways to access information stored: Query and Scan. A Query will rely on the primary-key to find information. Query can point directly to a particular item (or set ot items) and retrieve them in a fast and efficient way. Scan, as the name suggests, will browse table items from start to finish.

Does DynamoDB scan return all items?

A Scan operation in Amazon DynamoDB reads every item in a table or a secondary index. By default, a Scan operation returns all of the data attributes for every item in the table or index. You can use the ProjectionExpression parameter so that Scan only returns some of the attributes, rather than all of them.


2 Answers

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 #ts > :daysPrior" 
};
like image 61
Sunil B N Avatar answered Oct 20 '22 21:10

Sunil B N


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.

like image 3
Yuci Avatar answered Oct 20 '22 23:10

Yuci