How to Query Secondary Indexes Using DynamoDBContext From the .NET SDK?

Related Articles

DynamoDB supports two types of secondary indexes

In the previous posts, we looked at how to query the indexes using the low-level API IAmazonDynamoDB.

In this blog we will learn how we can query the index using the high level API, IDynamoDBContext.

The high-level API has limitations in how it can interact with the index, and often for more advanced-level functionality, you’ll need to switch to a low-level API for querying.

See the blog post below to learn how you can query data from DynamoDB using the .NET SDK.

5 ways to query data from Amazon DynamoDB using .NET

A query is an essential operation in DynamoDB. It allows you to filter and select items in your database based on your application and user needs. When moving to DynamoDB from more traditional relational databases like SQL Server, you need to understand the different ways you can retrieve data…

DynamoDBContext QueryAsync and indexes

God IDynamoDBContext there is QueryAsync Method, used to query objects from DynamoDB.

It supports passing additional configuration using the optionalDynamoDBOperationConfig resist.

God DynamoDBOperationConfig There is an asset IndexName, to specify the name of the index on which to run the query. This is an optional field, and if not specified the query operation is performed on the base table.

Querying either index, the global secondary index or the local secondary index, using the high-level API works the same, as shown below.

public async Task<IEnumerable<WeatherForecastListItem>> QueryGSIIndexUsingDynamoDBContext(DateTime dateTime)
    return await _dynamoDbContext
            new DynamoDBOperationConfig()
                IndexName = "Date-CityName-with-Temp-index",
                OverrideTableName = nameof(WeatherForecast)

All you have to do is specify the IndexName as a part of DynamoDBOperationConfig.

You can specify the table name using OverriderTableName Purchase or use the DynamoDBTable attribute. See the .NET DynamoDB SDK: Understanding Table Naming Conventions and the DynamoDBTable Attribute for more information.

DynamoDBContext LoadAsync and indexes

God DynamoDBContext there is LoadAsync A method used to load a specific item from a DynamoDB table.

Although the method syntax supports passing in the same DynamoDBOperationConfigIt doesn’t work for LoadAsync method.

The primary key values ​​must be unique in the DynamoDB table. However, within an index, the key values ​​can be duplicated.


In a DynamoDB table, each key value must be unique. However, the key values ​​in a global secondary index need not be unique

Trying to load a particular item given a set of keys makes no sense in an index for the same reason. Because you can get back more than one item

for the same reason LoadAsync method, although it supports the same DynamoDBOperatrionConfig object, it ignores the IndexName attribute. The query operates on the base table using its primary keys.

This is poor design on the SDK side, supporting the same operation configuration for both Load and Query methods. There is an existing problem for this and you can Follow it here for more details.


Popular Articles