0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks Matthew, I've updated the question. you're absolutely right. The thing is, I've already got the list of ids from another query which returned people I'm following, and yes those ids are the primary keys. Now since I have the primary and the sort key it would have been easy to query only one of them but when it comes to a list it gets difficult.
If you already have the Partition Key and the Sort Key, then you should use a BatchGetItem request.
Besides, I'm assuming making request for each id could becomes very expensive. I guess NoSql is not a very perfect fit for my very relational based application, or maybe I need to redesign the database structure. not sure.
would it be possible to feed a list to a BatchGetItem? I've never used it .. I have a look
So you have only a part of the sort key? If that's the case, then you must use a query for each partition key value that you have. On the other hand, if you can add a new attribute to your table, you could create a GSI that would help you. (Technically, you could also use scan, but that would be slower and more expensive than a query.)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.