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'
};
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"
};
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.
Related
Using "aws-sdk": "^2.1063.0" and nodejs 12
Inside my lambda I am doing an update to a dynamodb table.
My table has a Primary key: JobUID type string and a Sort key type string.
My parameters look like this:
var params = {
TableName: tableName,
Key: {
"JobUID": payload.JobUID,
"TimeStamp": payload.TimeStamp
},
UpdateExpression:
"set #HasResponse = :v_HasResponse, #ResponseTimeStamp = :v_ResponseTimeStamp, #Recommendation = :v_Recommendation, #ThreadRepComment = :v_ThreadRepComment",
ExpressionAttributeNames: {
"#HasResponse": payload.HasResponse,
"#ResponseTimeStamp": payload.ResponseTimeStamp,
"#Recommendation": payload.Recommendation,
"#ThreadRepComment": payload.ThreadRepComment,
},
ExpressionAttributeValues: {
":v_HasResponse": payload.HasResponse,
":v_ResponseTimeStamp": payload.ResponseTimeStamp,
":v_Recommendation": payload.Recommendation,
":v_ThreadRepComment": payload.ThreadRepComment,
},
// returns only the affected attributes, as they appeared after the update
ReturnValues: "UPDATED_NEW"
};
I have printed out the payload.JobUID and payload.TimeStamp in the log so I know they are what expect.
The latest row in the table has JobUID and TimeStamp exactly as I printed them out.
I want to update the 4 properties in the expression attribute names.
I am getting the error "ValidationException: The provided key element does not match the schema"
I have looked on the web and in SOF at examples of updates and I cannot seem to get this to work.
what is wrong with my key values.
The update call looks like this. Super simple
var returnValue = await dynamo.update(params).promise();
I also tried
Key: {
JobUID: {"S": payload.JobUID},
TimeStamp: {"S":payload.TimeStamp}
},
So this is what I found works:
var params = {
TableName: tableName,
Key: {
JobUID: payload.JobUID,
TimeStamp: payload.TimeStamp
},
UpdateExpression:
"set HasResponse = :v_HasResponse, ResponseTimeStamp = :v_ResponseTimeStamp, Recommendation = :v_Recommendation, ThreadRepComment = :v_ThreadRepComment",
ExpressionAttributeValues: {
":v_HasResponse": payload.HasResponse,
":v_ResponseTimeStamp": payload.ResponseTimeStamp,
":v_Recommendation": payload.Recommendation,
":v_ThreadRepComment": payload.ThreadRepComment || "",
},
// returns only the affected attributes, as they appeared after the update
ReturnValues: "UPDATED_NEW"
};
var returnValue = await dynamo.update(params).promise();
If there is a property is is null or empty, in my case the ThreadRepComment could be null or empty so you need to handle that.
Lets say i have an entity with this model:
{
id: 'apples',
createdAt: 'some date'
rate: 430,
side: 'SELL',
status: 'OPEN',
GSI1: 'SELL#OPEN#430'
GSI2: 'apples'
}
i want to query using the GSI attributes of GSI2 beign the hash and GSI1 being the range.
The query im looking for is get all apples(GSI2) where GSI1 begins with SELL#OPEN and >= SELL#OPEN#430 so basically im trying to get all apples being sold for 430 or greater and are open.
Please how do i go about this using dynamodb query?
what i have done is:
params = {
TableName: process.env.ORDERS_TABLE_NAME,
IndexName: "GSI2_GSI1",
KeyConditionExpression: `GSI2 = :item and ((begins_with(GSI2, :sideStatus) and >= :baseRate)`,
ExpressionAttributeValues: {
":item": `apple`,
":baseRate": `SELL#OPEN#${rate}`,
":sideStatus": "SELL#OPEN",
},
};
thanks
You can only operate on the Key attributes in the key condition expression. These parameters should do what you want because you have all the information in the GSI1 attribute
params = {
TableName: process.env.ORDERS_TABLE_NAME,
IndexName: "GSI2_GSI1",
KeyConditionExpression: 'GSI2 = :item and GSI1 BETWEEN :lower AND :upper',
ExpressionAttributeValues: {
":item": `apple`,
":lower": `SELL#OPEN#430`,
":upper": "SELL#OPEN#999", // you can probably also use "SELL#OPEN$" because $ is the character following # in ascii order
},
};
Note this assumes that your rate in the GSI1 attribute is left padded with 0s. You need the string to be sorted in the same order as the numbers so if the rate is 10 then you need to store SELL#OPEN#010. (Note you might need more leading 0s depending on the maximum rate.)
What is the proper syntax to query database entries whose sort key starts with a specific string?
I believe it's something along the lines of
const query_params = {
TableName: 'my_table',
Key: {
my_primary_key: 'a_primary_key_value',
},
FilterExpression: "my_sort_key begins_with :string",
ExpressionAttributeValues: {
":string": "starts_with_substring"
}
};
Followed by a dynamoDb.get(query_params, ..., but that is not quite right. I am getting an ValidationException: The provided key element does not match the schema error.
According to the SDK query documentation, your query params should look like this
{
TableName: "my-table",
KeyConditionExpression: "#pk= :pk And begins_with(#sk, :sk)",
ExpressionAttributeValues: {
":pk": "a_primary_key_value",
":sk": "starts_with_substring"
},
ExpressionAttributeNames: {
"#pk": "my_primary_key",
"#sk": "my_sort_key"
}
}
You'll also need to change dynamoDb.get() to dynamoDb.query().
The Situation:
I have created a DynamoDB which is accessed by a Lambda script.
Accessing data with query and get are working fine - also on Global Secondary Indexes
The Challenge:
For one use case I need to query for a Global Seconday Index and filter the result to exclude items when they have a certain value for another attribute
What I'm currently trying to do (sample code):
const paramsQueryAndFilter = {
TableName: "Sample",
//Search for items based on Global Secondary Index for Attribute 1
IndexName: "Attribute_1-index", // is a Global Secondary Index
KeyConditionExpression: "Attribute_1 = :Attribute_1",
ExpressionAttributeValues: {
":Attribute_1": "70"
},
//Filter items
ExpressionAttributeNames: {
"Attribute_2": "Attribute_2"
},
//Filter Attribute 2 to exclude value "excludeValue"
FilterExpression: "Attribute_2 ne :excludedValue",
ExpressionAttributeValues: {
":excludedValue": "false"
}
}
What I'm looking for: I hope to get some idea on how to correctly filter out items based on Attribute_2.
Use queryfilter in your above code
KeyConditionExpression: "Attribute_1 = :Attribute_1",
ExpressionAttributeValues: {
":Attribute_1": "70"
},
QueryFilter: {
'<Attribute_1>': {
ComparisonOperator: "EQ", /* required */
AttributeValueList: [ "somevalue", "somevalue2" ]
}
Refer to documentation: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#query-property
Querying dynamoDB table with node.js. The DynamoDB table has key of Timestamp, represented by an integer. In this case, I left :timestampStart as 1 and timestampEnd as 10 as an example.
var params = {
TableName: "Table2",
KeyConditionExpression:"Timestamp = :ts BETWEEN :timestampStart AND :timestampEnd",
ExpressionAttributeValues: {
":ts":"Timestamp",
":timestampStart": 1,
":timestampEnd": 10
}
};
The :ts is not correct, I can see this. I want to return any rows found with a Timestamp value between timestampStart and timestampEnd.
Error message:
"errorMessage": "Invalid KeyConditionExpression: Syntax error;
token: \"BETWEEN\", near: \":ts BETWEEN :timestampStart\"",
If Timestamp is the partition key and not the sort key. Then, you have two problems:
In a Query operation, you cannot perform a comparison test (<, >, BETWEEN, ...) on the partition key. The condition must perform an equality test (=) on a single partition key value and, optionally, one of several comparison tests on a single sort key value. For example:
KeyConditionExpression: 'HashKey = :hkey and RangeKey > :rkey'
You have a syntax error in your KeyConditionExpression, obviously. Keep in mind that Timestamp is a reserved word in DynamoDB. So, you'll have to use ExpressionAttributeNames for that:
(Assuming you have an Id partition key and Timestamp sort key)
var params = {
TableName: "Table2",
KeyConditionExpression: "Id = :id AND #DocTimestamp BETWEEN :start AND :end",
ExpressionAttributeNames: {
'#DocTimestamp': 'Timestamp'
},
ExpressionAttributeValues: {
":id": "SOME VALUE",
":start": 1,
":end": 10
}
};