adimona
adimona

Reputation: 109

Query from a [ List ] of DynamoDB items from an AWS Lambda function

How do I query from a list? I simply want to return the name of all these people.

var MylistOfIds = ["userid1","userid2","userid3", ... ]

My code doesn't work at.

var params = {
    TableName: "myTableName",
    ProjectionExpression: "Name, Post",
    KeyConditionExpression: "begins_with(#sk, :skv)",
    FilterExpression: "#tp in (:mylist)",

    ExpressionAttributeNames: {
        "#tp": "userId",
        "#sk": "Sortkey",

    },
    ExpressionAttributeValues: {

        ":mylist": { L :  MylistOfIds }, 
        ":skv": { "S": "Post" },

    }
};

here's the query part

let data1 = await dynamodb.query(params).promise();

          const items = data1.Items.map(
            (dataField) => {

                return {

                  name : dataField.Name.S
                  post : dataField.Post.S

                };
            }
        );
                  callback(null, { "user_name": items });

I've also tried this answerer here without any luck :(

Here is my current table structure :

|-----Primary-----|------SortKey-----|-----Name------|----Post-----|
    userid1             Post:345            Bob        Lorem Ipsum
    userid1             Post:457            Bob        Lorem ...
    userid2             Post:678            Rose       asdf .....
    userid3             Post:987            Jack       texte...
    userid3             Post:345            Jack       Loremimplsu.

Upvotes: 0

Views: 838

Answers (1)

Matthew Pope
Matthew Pope

Reputation: 7669

You have no partition key in your key condition expression. A KeyConditionExpression must always have the partition key.

You must specify the partition key name and value as an equality condition. (source)

Furthermore, if userId is your partition key, then you cannot use it in your filter expression.

A filter expression cannot contain partition key or sort key attributes. You need to specify those attributes in the key condition expression, not the filter expression. (source)

In order to do a begins_with query, assuming UserId is your partition key, you will need to make a separate request for each value of UserId.

If you need more help, you should update your question to include your current table structure.

Upvotes: 1

Related Questions