amazon-web-servicesaws-lambdaamazon-dynamodbdynamodb-queriesamazon-dynamodb-index

How to do a dynamic search using multiple attributes in DynamoDB?


I have a user table with following attributes:

I want to query the table with any combination of these attributes and that too partial .

For example: Input - Firstname- "bh" and lastname - "ta" Output -A Record with FirstName "aBHishek" , lastName"gupTA"

How do I do this query without using scan? Does using scanconditions with contains means the read operation is less costly?

I have tried this in C#:

var conditions = new List<ScanCondition>();

if (!string.IsNullOrEmpty(user.FirstName))
{
    var firstName = new ScanCondition("SearchFirstName", ScanOperator.Contains, user.FirstName.ToLower());
    conditions.Add(firstName);
}
 

if (!string.IsNullOrEmpty(user.LastName))
{
    var lastName = new ScanCondition("SearchLastName", ScanOperator.Contains, user.LastName.ToLower());
     conditions.Add(lastName);
}

var allUsers = await _context.ScanAsync<OpenOrderUser>(conditions).GetRemainingAsync();
return allUsers;

it does the trick but I want to move away from Scan and use Query, any suggestions? is this approach optimal?


Solution

  • Another option you have to retrieve data from an Amazon DynamoDB table when using the AWS SDK for .NET V3 is to use PartiQL. This lets you use SELECT statements to retrieve data from a table in Amazon DynamoDB. Its similiar to SQL.

    You can read more here:

    PartiQL select statements for DynamoDB

    YOu can find full .NET Code example here:

    https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/dotnetv3/dynamodb/scenarios/PartiQL_Basics_Scenario/PartiQL_Basics_Scenario/PartiQLMethods.cs