简体   繁体   中英

How to select nested attribute in scan in DynamoDB?

I realize this is answered in the documentation (basically, "use the dot syntax"), but I'm still missing something. I'm using the .NET SDK and need to be able to select just a few attributes from a scan, one of which is a boolean inside a Map attribute. Note that I'm not trying to filter the results. I want all of the items, but I only want some of the attributes returned to me.

var config = new ScanOperationConfig
{
    AttributesToGet = new List<string> { "foo.bar" },
    Select = SelectValues.SpecificAttributes
};
var search = Table.Scan(config);
var documents = await search.GetRemainingAsync();

This code gets me the items I expect, but it's missing the "foo.bar" attribute. I know I can select the entire foo object, but I'm trying to minimize the amount of data handed back. I don't want the other attributes inside the foo object.

The relevant attribute of the item has the following JSON format:

{
    "foo": {
        "bar": true
    }
}

I checked spelling, case sensitivity, etc. to no avail. Any idea what's wrong?

Instead of using Table.Scan , use the AmazonDynamoDBClient and you get more options.

The Client's ScanAsync method takes a ScanRequest which has a ProjectionExpression string. This is not present on the ScanOperationConfig class and that was the source of confusion.

Use the ProjectionExpression like so:

var scanRequest = new ScanRequest(Table.TableName)
{
    ProjectionExpression = "foo.bar"
};

According to the documentation on ProjectionExpression :

ProjectionExpression replaces the legacy AttributesToGet parameter.

I didn't realize AttributesToGet was legacy until finally looking at the client for a totally unrelated problem and happened to find my answer to this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM