Scan a String Set(SS) that contains a substring - amazon-dynamodb

Suppose I have the following schema:
id: S
my_list: SS
With the following example record:
{
"id": "auniquestring",
"list": [
"item1",
"item11",
"item111",
"item2",
"item22",
"item3"
]
I am scanning the table with the following params:
params = {
ExpressionAttributeValues: {
":v1": {
S: item_substring
}
},
FilterExpression: "contains(list, contains(list_item, :v1))",
TableName: "MyItemsTable"
};
The basic idea is to return the record that contains a string in a list of strings, containing the queried substring.
Answer in JavaScript would be preferred.

I think your FilterExpression is not correct. Why are you nesting contains?
Have you tried FilterExpression: "contains(list,:v1)"?

Related

Why do I get empty list when doing filter?

I have a table 'test-table':
id (string) - primaryKey
type (string)
I have items like this in that table, for example:
34 AWC
56 BDE
I want to do scan table and filter by type:
I use:
async getItems(typeInput) {
const params: ScanCommandInput = {
TableName: "test-table",
FilterExpression: "type in (:type)", // also tried with type = :type
ExpressionAttributeValues: { ":type": { "S": typeInput } },
};
return await dynamodbdocumentclient.send(new ScanCommand(params));
}
I get as a result empty Items. Why ?
You appear to be using the DocumentClient, which automatically marshalls attribute values from their native JavaScript type. You do not need to wrap all values in {'S': 'xxx'}, {'N': '999'}, etc. Use ExpressionAttributeValues: { ":type": typeInput }.

Dynamodb - Condition Expression with ExpressionAttributeNames not working

Am trying to do a conditional update and since the field name is a reserved keyword, am using ExpressionAttributeNames to pass in the column name for the condition. The update part isn't working if i use the ExpressionAttributeNames. Any thoughts?
Update: {
Key: {
"pk": "sb#prd#" + productId,
"sk": productId.toString()
},
TableName: 'test',
ConditionExpression: '#minQty >= :minQty',
UpdateExpression: "set sold_count = :sold_count",
ExpressionAttributeNames: {
"#minQty" : "variant[0].items[0].availableQty"
},
ExpressionAttributeValues: {
":sold_count" : 1,
":minQty" : 1
},
}
The aliases defined in ExpressionAttributeNames can only be a single attribute name - they cannot be full paths like variant[0].items[0].availableQty.
You can do something like:
ConditionExpression: '#v[0].#i[0].#a >= :minQty',
ExpressionAttributeNames: {
"#v" : "variant",
"#i" : "items",
"#a" : "availableQty"
},

ConditionalExpression DynamoDb Insert

I have the below data schema for my DynamoDb table. I am trying to append list subscribers on the condition if input = name (ex: input = my-topic2). There can be many maps in the “topics” list and I need to search for the map where the name = input and from there add the subscriber to that topic.
{
“server-id”: “123345678”,
“server-name”: “my-server”
“topics”: [
{
“name”: “my-topic”,
“subscribers”: []
},
{
“name”: “my-topic2”,
“subscribers”: [] //This is what I need to append on a condition that the input = “my-topic2”
}
]
}
I have the current following paeans I am using which appends “my-topic” subscribers.
params = {
ExpressionAttributeNames: {
"#T": "topics",
"#S": "subscribers"
},
ExpressionAttributeValues: {
":vals": [
message.author.id
]
},
Key: {
'server-id': serverID
},
ReturnValues: "ALL_NEW",
TableName: tableName,
UpdateExpression: "SET #T[0].#S = list_append(#T[0].#S, :vals)"
};

DyanamoDB SCAN with nested attribute

Can I scan DynamoDB by 'order.shortCode', in the given example. The console is indicating I can't with dot notation, and I can't find any documentation on it.
{
"key2": "cj11b1ygp0000jcgubpe5mso3",
"order": {
"amount": 74.22,
"dateCreated": "2017-04-02T19:15:33-04:00",
"orderNumber": "cj11b1ygp0000jcgubpe5mso3",
"shortCode": "SJLLDE"
},
"skey2": "SJLLDE"
}
To scan by a nested attribute, you should use ExpressionAttributeNames parameter to pass each path component (i.e. order and shortCode) separately into FilterExpression like shown below:
var params = {
TableName: 'YOUR_TABLE_NAME',
FilterExpression: "#order.#shortCode = :shortCodeValue",
ExpressionAttributeNames: {
'#order': 'order',
"#shortCode": "shortCode"
},
ExpressionAttributeValues: {
':shortCodeValue': 'SJLLDE'
}
};
dynamodbDoc.scan(params, function(err, data) {
});
Here is a link to documentation explaining this:
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html#Expressions.ExpressionAttributeNames.NestedAttributes

How to create and populate lists in DynamoDB

I wish to create an Item in DynamoDB that is a list. This is my code:
var list_update_params = {
TableName: "table01",
Key: {
"MachineID": {
"S": MachineID
},
"Hour": {
"S": Hour
}
},
UpdateExpression: "set var01_list = list_append(var01_list, :ot)",
ExpressionAttributeValues: {
":ot": {"L": [{"N": var01}]}
},
ReturnValues: "NONE"
};
dynamodb.updateItem(list_update_params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log("Updated List to DynamoDB");
});
The problem is list_append expects the attribute var01_list to already be present, but I wouldn't know at the first insert. Is there a technique where it'll let me create an insert a List attribute if one doesn't exist and append to it in later calls?
Got the answer from a similar post here.
UpdateExpression: "set var01_list= list_append(if_not_exists(var01_list, :empty_list), :h)",
ExpressionAttributeValues: {
":h": {"L": [{"N":var01}]},
":empty_list": {"L": []}
},
The key was using if_not_exists with list_append. Didn't know that could be done in this matter

Resources