2

I am using Dynamodb.net here. How can I add multiple scan conditions so that the data is filtered out based on those conditions. I am using the below code:

var creds = new BasicAWSCredentials(awsId, awsPassword);
var dynamoClient = new AmazonDynamoDBClient(creds, awsDynamoDbRegion);
var context = new DynamoDBContext(dynamoClient);
List<ScanCondition> conditions = new List<ScanCondition>();
//  conditions.Add(new ScanCondition("Id", ScanOperator.Equal, myId));
conditions.Add(new ScanCondition("name", ScanOperator.Equal, myName));
var response = await context.ScanAsync<Data>(conditions).GetRemainingAsync();
return response;

In my code above if I add 2 scan conditions, it does not works. But does work with one condition. Not sure if what I am doing wrong here.

1 Answer 1

9

Your code looks OK, with one caveat: scan conditions are for non-key attributes.

I'm going to go out on a limb and assume that Id is the partition key (or perhaps sort key) of your table. If that's true then that's why you can't use it in a scan condition. You can have multiple Scan conditions added but they must all be for non-key attributes.

In order to specify key conditions, you must use a Query operation, not a Scan.

Assuming your table only has a Primary key and no Sort key, then the example below should work. However, if the table has a sort key as well then your query must include that as well so the example below would need to be modified slightly.

var creds = new BasicAWSCredentials(awsId, awsPassword);
var dynamoClient = new AmazonDynamoDBClient(creds, awsDynamoDbRegion);
var context = new DynamoDBContext(dynamoClient);


var opConfig = new DynamoDBOperationConfig();
opConfig.QueryFilter = new List<ScanCondition>();
opConfig.QueryFilter.Add(new ScanCondition("name", ScanOperator.Equal, myName));
var response = await context.QueryAsync<Data>(myId, opConfig).GetRemainingAsync();
return response;
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Mike I believe yes I have to use Query and not scan. One more question though. Do you know how can we incorporate OR condition together with the And conditions. Say I have 3 AND conditions and one OR condition. The scenario I have is that I am using 3 AND conditions which are my page filters and the OR condition which is my page search. I have one API which I am calling for this. Because of very less information on dynamodb .net what I cam currently doing is using SCAN for AND conditions then using c# Linq for OR. Any how I could club AND and OR together? Thanks
It is possible to include boolean logic in filter expressions using the low level client. See: docs.aws.amazon.com/amazondynamodb/latest/developerguide/…
It looks like the Async client has limited support for boolean logic in conditional statements: docs.aws.amazon.com/sdkfornet/v3/apidocs/…
Yes Mike I think for now i would continue to use filter with OR in my Linq. Thanks for your help.
You have probably moved on since this was written ;) I think the query language for dynamoDB is a bit limited compared to other databases like mongoDB, neo4J or relational databases (SQL).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.