haritb
haritb

Reputation: 51

Dynamodb query operations

Is it possible to work with "OR" , "AND " Query operations in DynamoDB?

I need to know if DynamoDB has something like "where fname = xxxx OR lname = xxxx" from SQL queries? in Rails

Thanks.

Upvotes: 4

Views: 3706

Answers (4)

Ankush Anshuman
Ankush Anshuman

Reputation: 28

Although there is no such thing as "OR", "AND", but you can still simulate sql queries using scan and filterexpression.

But remember that if you are using scan, then it means whole table will be fetched and then processed, also scan doesn't always scan whole table in one iteration. so you might miss out some items. so this method is generally avoided as it is very costly. but here is piece of python3 code using boto3 api that can simulate the sql query you want

response = table.scan(
FilterExpression=Attr('fname').eq('xxxxx') | Attr('lname').eq('xxxxx'))

filterexpression also different operatiors like &,~

Upvotes: 0

arjun kori
arjun kori

Reputation: 1120

No it not possible to Use 'OR' Operator, For example in KeyConditionExpression: '#hashkey = :hk_val AND #rangekey > :rk_val',

it Uses And Operator for matching the for bot HASH and RANGE Key.

There fore we cant use OR in Dynamo Db.

Upvotes: 0

Ashwin Patti
Ashwin Patti

Reputation: 598

As BCoates mentioned the answer is NO.

If you want consistent read then you can't use BatchGetItem.

Upvotes: 1

BCoates
BCoates

Reputation: 1518

In general, no.

DynamoDB only allows efficient lookup by primary(hash) key, plus optionally a range query on the "range key". Other attributes are not indexed.

You can use a Scan request to read an entire table filter by a set of attributes, but this is a relatively expensive and slow option for large tables.

You can simulate AND by creating a primary key that includes both values to be queried, and OR by creating duplicate tables that each use one attribute as their primary key, and querying both tables in parallel with BatchGetItem

Upvotes: 6

Related Questions