JDiPierro
JDiPierro

Reputation: 802

Using nested fields in the projection of a DynamoDB GSI

I've got a Dynamo table storing documents that look like this:

{
  "guid": "<some UUID>"
  "created_at": 1550778260030,
  "display_name": "person",
  "updated_at": 1550778260030,
  "info": {
    "locked": false,
    "confirmed": true,
    "deactivated": false,
    "email": "[email protected]"
  }
}

The table has a Global Secondary Index managed by Terraform defined as such:

    global_secondary_index {
        name = "display_name_index"

        hash_key = "display_name"

        projection_type = "INCLUDE"
        non_key_attributes = [
            "updated_at",
            "info.email",
            "created_at"
        ]
    }

However when I query the table the info.email field isn't returned:

aws dynamodb query \
  --table-name "accounts" \
  --index-name "display_name_index" \
  --key-condition-expression "display_name = :display_name" \
  --expression-attribute-values '{":display_name":{"S":"person"}}'
{
    "Count": 1,
    "Items": [
        {
            "created_at": {
                "N": "1550778260030"
            },
            "display_name": {
                "S": "person"
            }
            "updated_at": {
                "N": "1550778260030"
            }
        }
    ],
    "ScannedCount": 1,
    "ConsumedCapacity": null
}

If I change the non_key_attributes to include info it returns the full info blob just fine, and I can use a projection-expression of info.email to retrieve that field just fine:

{
    "Count": 1,
    "Items": [
        {
            "info": {
                "M": {
                    "email": {
                        "S": "[email protected]"
                    }
                }
            }
        }
    ],
    "ScannedCount": 1,
    "ConsumedCapacity": null
}

The Dynamo docs do specify that index keys have to be top-level, but they don't mention anything about non-key attributes in a projection having to be top-level. Therefore I'd assume that anything that works in a projection-expression should work in an index projection, but that seems to not be the case?

Am I doing something wrong with this index definition or the query? Or does Dynamo just not support nested non-key attributes as part of an index's projection?

Upvotes: 8

Views: 2530

Answers (2)

Gagan Chouhan
Gagan Chouhan

Reputation: 342

In simple words nested attribute can not be used as a GSI projection. It is not supported in DDB yet.

Upvotes: 1

Jovana
Jovana

Reputation: 21

I walked into the same thing. I see there are no answers jet to your question. Not sure I have the right answer, but maybe it can help you out.

First of all, I think it's very weird creating GSI the API allows you to add a projection of "info.email" (this will also visible on the index overview page) but can never be retrieved again.

I found out when creating a GSI you are stuck on the attributes you have provided. On the other hand, creating LSI you can use the attributes you have provided while creating the LSI.

You can found a little about this in this document (search for "Projected Attributes"): https://docs.amazonaws.cn/en_us/amazondynamodb/latest/developerguide/SecondaryIndexes.html

I hope you can do something with this info.

Upvotes: 0

Related Questions