im using aws dynamo db to fetch all datas from a userkyc table where kyc_status should be "A" and "D". Below code i can only use either "A" or "D". How do i fetch all data for both conditions. Help will be appreciated!
var params = {
TableName: "user_kyc",
ProjectionExpression: "email, uid, kyc_status",
KeyConditionExpression: "#kyc_status = :kyc_status",
ExpressionAttributeNames: {
"#kyc_status": "kyc_status",
},
ExpressionAttributeValues: {
":kyc_status": "A"
},
FilterExpression: "#kyc_status = :kyc_status"
};
You could change your params to something like:
var params = {
TableName: "user_kyc",
ProjectionExpression: "email, uid, kyc_status",
KeyConditionExpression: "#kyc_status = :kyc_status1 or #kyc_status = :kyc_status2",
ExpressionAttributeNames: {
"#kyc_status": "kyc_status",
},
ExpressionAttributeValues: {
":kyc_status1": "A",
":kyc_status2": "D"
}
};
Here I added another variable, now there is :kyc_status1 and :kyc_status2, and used this now variable in KeyConditionExpression to do a or expression.
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
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)"?
This is my handler function.
const updateOrder = (request) => {
const id = request.pathParams.id;
const orderObject = request.body;
if(!id || !orderObject)
throw new Error ('Please provide required id and object in order to update the order')
const params = {
TableName: "pizza-order",
Key: {
"orderId": id
},
UpdateExpression: "SET #n = :n, #a = :a ",
ExpressionAttributeNames: {
"#n": "pizza",
"#a": "address"
},
ConditionExpression: '' ,
ExpressionAttributeValues: {
":n": orderObject.pizza,
":a": orderObject.address,
},
};
return docClient.update(params).promise()
}
And the order Object is:
{
"pizza":"THe outstanding pizza",
"Address": "feni"
}
And it returns error like this. I don't know why it is happening. Very new to serverless development. anyone know the solution?
It was a very silly mistake. First in the order Object there is a typo:
{
"pizza":"THe outstanding pizza",
"address": "feni" //previously it was Address: "feni
}
And the second reason is ConditionExpression Can not be empty.
const updateOrder = (request) => {
const id = request.pathParams.id;
const orderObject = request.body;
if(!id || !orderObject)
throw new Error ('Please provide required id and object in order to update the order')
const params = {
TableName: "pizza-order",
Key: {
"orderId": id
},
UpdateExpression: "SET #n = :n, #a = :a ",
ExpressionAttributeNames: {
"#n": "pizza",
"#a": "address"
},
ConditionExpression: '' , // this can not be empty
ExpressionAttributeValues: {
":n": orderObject.pizza,
":a": orderObject.address,
},
};
return docClient.update(params).promise()
}
Can't get query on DynamoDB null attributes.
I have below query, I am getting NameError: name 'true' is not defined
const filter = {
FilterExpression: 'id = :null',
ExpressionAttributeValues: {
':null': true,
},
};
You need to specify the type of null as well.
Try this :
const filter = {
FilterExpression: 'id = :null',
ExpressionAttributeValues: {
':null': {BOOL: true}
}
};
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)"
};