Glenn Slaven
Glenn Slaven

Reputation: 34183

Can I use the DynamoDB .NET Object Persistence Model when I have a Global Secondary Index?

I have a table in Dynamo with a hash & range index plus a secondary global index with just a hash. If I try to Query or Save an object I get the following error:

Number of hash keys on table TableName does not match number of hash keys on type ObjectModelType

(Replacing TableName and ObjectModelType with the actual table and model type)

I have the hash properties (both the primary and secondary) decorated with DynamoDBHashKey

Googling the error turns up exactly zero results

Update: Ok, so not exactly zero, obviously it now returns this question!

Update the second: I've tried using the helper API & it works just fine, so I am assuming at this point that the Object Persistence Model doesn't support Global Secondary Indexes

Upvotes: 1

Views: 2220

Answers (2)

Nick Pirocanac
Nick Pirocanac

Reputation: 161

Thank you Brendon for that tip! I was able to adapt it to get deserialization of a multiple result set.

var queryFilter = new QueryFilter(indexedColumnName, QueryOperator.Equal, targetValue);
Table table = Table.LoadTable(client, Configuration.Instance.DynamoTable);
var search = table.Query(new QueryOperationConfig { IndexName = indexName, Filter = queryFilter });

List<MyObject> objects = new List<MyObject>();
List<Document> documentSet = new List<Document>();
do
{
    documentSet = search.GetNextSetAsync().Result;
    foreach (var document in documentSet)
    {
        var record = JsonConvert.DeserializeObject<MyObject>(document.ToJson());
        objects .Add(record);
    }
} while (!search.IsDone);

Thanks x1000!! :)

Upvotes: 1

Brendon
Brendon

Reputation: 111

I encountered the same problem and found FromQuery worked, although QueryFilter is actually from the DocumentModel namespace:

var queryFilter = new QueryFilter(SecondaryIndexHashKeyColumn, QueryOperator.Equal, "xxxx");
queryFilter.AddCondition(SecondaryIndexRangeKeyColumn, QueryOperator.LessThan, DateTime.Today);
var items = context.FromQuery<MyItem>(new QueryOperationConfig { IndexName = SecondaryIndexName, Filter = queryFilter }).ToList();

Upvotes: 2

Related Questions