SOLUTION FOUND AT EDIT
I'm using the DynamoDBContext class in ASP.NET Core (Object persistence model) and have items stored in my Inventory table in this form:
{
"itemID": 0,
"stockCount": 0,
"testAttributes": {
"color": "white",
"additionalProp2": "string",
"additionalProp3": "string"
}
}
Which are mapped to my C# Objects like so:
[DynamoDBTable("Inventory")]
public class ItemDto
{
[DynamoDBHashKey]
public int ItemID { get; set; }
public int StockCount { get; set; }
public Dictionary<string,string> TestAttributes { get; set; }
}
I'm attempting to scan this table and find any item which is the color white. The TestAttributes are not constant so I am using this mapping style. Examples of what I have tried thus far include:
Dictionary<string, string> x = new Dictionary<string, string>()
{
{"color", "white"}
};
var conditions = new List<ScanCondition>()
{
new ScanCondition("TestAttributes", ScanOperator.Contains, x.ElementAt(0))
};
var result = await Context.ScanAsync<ItemDto>(conditions).GetRemainingAsync();
The Documentation says the scan parameter (in this case x.ElementAt(0)) must be the same as the attribute you are scanning on, therefore I used this construction style. In other posts I have seen some cases where "TestAttributes.color" may work but none of these accesses with the correct scanning object type have worked for me.
When I do match the correct parameter type I see you cannot use the contains operator on M type objects, which leads me to believe the TestAttributes.color accessing style should work in some way or another - but have not found that method as of yet. I would like to continue using the Object Persistence model throughout, so using DynamoDBContext rather than DynamoDBClient.
Any advice would help, the documentation is terrible and I have not found the answer for this issue anywhere. Thanks.
EDIT W/ SOLUTION: From an obscure aws forum a dev mentioned that ScanFilter was implemented before mappings were added, therefore ScanFilter cannot work with scanning with conditions for mappings. Therefore, you must not use DynamoDBContext to do this kind of scan, but rather use the DynamoDBClient class and use an Expression Filter like so:
var expressionValues = new Dictionary<string, AttributeValue>();
expressionValues.Add(":att", new AttributeValue{ {S="white"} });
var request = new ScanRequest{
TableName = "Inventory",
FilterExpression = "Attributes.color = :att",
ExpressionAttributeValues = expressionValues};
var response = await Client.ScanAsync(request);
You can then loop through the items matching the scan conditions like so:
foreach(var item in response.Items)
{
//Do something with item here, below just gets my itemID field
var itemID = item["ItemID"].S;
}