Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OR conditional expression for Query in DynamoDB NodeJS

I'm trying to get a list of entries from DynamoDb using FilterExpression. I'd like to return value if at least one of the statements are TRUE. But for some reason looks like AND is a default conditional operator and my result is empty. How to change default conditional operation to OR in NodeJS?

            params = {
                TableName: '......',
                IndexName : 'user_id-index',
                KeyConditionExpression: 'user_id = :user_id',
                FilterExpression: 'contains (email, :key) OR contains (name, :key)',
                ExpressionAttributeValues: {
                    ":user_id": userId,
                    ":key": item_to_search
                }
            };
like image 467
levo4ka Avatar asked Feb 23 '26 04:02

levo4ka


1 Answers

Your problem is that NAME is a reserved word in DynamoDB. So, you'll have to use ExpressionAttributeNames for that:

params = {
  TableName: '......',
  IndexName: "user_id-index",
  KeyConditionExpression: "user_id = :user_id",
  FilterExpression: "contains (email, :key) OR contains (#name, :key)",
  ExpressionAttributeNames: {
    "#name": "name"
  }
  ExpressionAttributeValues: {
    ":user_id": userId,
    ":key": item_to_search
  }
};
like image 75
Khalid T. Avatar answered Feb 25 '26 22:02

Khalid T.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!