I'm using the AWS.DynamoDB.DocumentClient with nodejs (lambda functions on AWS)
I've to a db called Translations, primary key Term, sort key Category.
Basically I'm trying to write data in like this:
{
Term: 'hello_there',
Category: 'common_phrases',
Translations: {
en-ca: 'Hello There!'
}
}
and I want to update Translations Map to perhaps add another language, or just modify the en-ca portion of the object.
I've tried the following:
let params = {
TableName: 'Translations',
Key: {
Term: 'hello_there',
Category: 'common_phrases',
},
UpdateExpression:
'SET Translations.#lang = list_append(Translations, :term)',
ExpressionAttributeValues: {
':term': 'french version',
},
ExpressionAttributeNames: {
'#lang': 'fr-ca',
},
}
But I get an error about the wrong operand type.
I've tried:
UpdateExpression:
'SET Translations.#lang = :term',
ExpressionAttributeValues: {
':term': 'french version',
},
ExpressionAttributeNames: {
'#lang': 'fr-ca',
},
But this overwrites the entire contents of the Translations field. Meaning that if I already had en-ca in there, now I would only have fr-ca.
I tried this as well, but also get an operand error:
{
UpdateExpression: 'ADD Translations :term',
ExpressionAttributeValues: {
':term': { 'fr-ff': 'frenchterm' },
},
Any suggestions on how to add/update items to the Translations Map?
Your SET expression was close, try something like this:
UpdateExpression:
'SET #trans.#lang = :term',
ExpressionAttributeValues: {
':term': 'french version',
},
ExpressionAttributeNames: {
'#trans': 'Translations`,
'#lang': 'fr-ca',
},
Related
I have the following schema:
{
balance: {
customData: string,
version: number
}
}
I need to update balance.customData only if version == 2. Once update operation succeeds, I need to increment version by 1.
I tried the following update transaction
Update: {
Key: {
'pk': pk,
'sk': sk
},
TableName: this.config.getTable(),
UpdateExpression: 'SET #balance.#customData = :customData, #balance.#version = #balance.#version + :incr',
ConditionExpression: '#balance.#customData.#version = :version',
ExpressionAttributeNames: {
'#customData': 'customData',
'#balance': 'balance',
'#version': 'version'
},
ExpressionAttributeValues: {
':customData': balance.customData,
':version': balance.version,
':incr': {'N': 1},
}
}
But when I run this, I get ValidationException: Invalid UpdateExpression: Incorrect operand type for operator or function; operator or function: +, operand type: M
I assume I'm not providing correct syntax to increment map value.
Even if I remove increment part
Update: {
Key: {
'pk': pk,
'sk': sk
},
TableName: this.config.getTable(),
UpdateExpression: 'SET #balance.#customData = :customData,
ConditionExpression: '#balance.#customData.#version = :version',
ExpressionAttributeNames: {
'#customData': 'customData',
'#balance': 'balance',
'#version': 'version'
},
ExpressionAttributeValues: {
':customData': balance.customData,
':version': balance.version
}
}
I still get ValidationError with reason Other, which is not very helpful. Can you please help me understand what I'm doing wrong? Thanks
I am trying to run a simple update query, but got an error when I tried to update the key of a map to the same value. Is there a technical reason this would be disallowed? or some kind of best-practice that I am violating by trying to do this?
Error:
ValidationException: Invalid UpdateExpression: Two document paths overlap with each other;
must remove or rewrite one of these paths; path one: [questions, What is xx?], path two: [questions, What is xx?]
Query object:
{
TableName: 'notesTable',
Key: { topic: 'My tooic' },
ExpressionAttributeNames: { '#qq': 'What is xx?', '#updq': 'What is xx?' },
ExpressionAttributeValues: { ':updans': 'new answer' },
UpdateExpression: 'REMOVE questions.#qq SET questions.#updq = :updans'
}
Multiple ways to deal with scenario when same key needs to be updated. Instead of removing and updating the same key, we can simply SET the key , which replaced the value anyhow.
So, simple way is to send different updateExpression each time.
const qq = "What is xx2?";
const updq = "What is xx?";
let expressionAttributeNames;
let UpdateExpression;
if (qq === updq) {
expressionAttributeNames = { "#updq": "What is xx?" };
UpdateExpression = "SET questions.#updq = :updans";
} else {
expressionAttributeNames = { "#qq": "What is xx1?", "#updq": "What is xx?" };
UpdateExpression = "REMOVE questions.#qq SET questions.#updq = :updans";
}
docClient.update(
{
TableName: "test",
Key: {
id: "My tooic",
},
ExpressionAttributeNames: expressionAttributeNames,
ExpressionAttributeValues: { ":updans": "new answer1" },
UpdateExpression: UpdateExpression,
},
function (error, result) {
console.log("error", error, "result", result);
}
);
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"
},
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
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