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.
adimona
  • 109
  • 2
  • 15

1 Answers1

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.

Matthew Pope
  • 7,212
  • 1
  • 28
  • 49
  • 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. – adimona Nov 19 '18 at 23:20
  • 1
    If you already have the Partition Key and the Sort Key, then you should use a [BatchGetItem](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchGetItem.html) request. – Matthew Pope Nov 19 '18 at 23:22
  • 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. – adimona Nov 19 '18 at 23:27
  • would it be possible to feed a list to a BatchGetItem? I've never used it .. I have a look – adimona Nov 19 '18 at 23:30
  • You have to do some processing of your list in order to get it in the correct format for a BatchGetItem request, but it is exactly what you should use if you need to look up multiple items and you know all of the items' partition and sort keys. – Matthew Pope Nov 19 '18 at 23:31
  • Could I use begin_with filter on a BatchGetItem? If the answer is yes they Yes I've got both partition and sort keys. – adimona Nov 19 '18 at 23:40
  • 1
    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.) – Matthew Pope Nov 19 '18 at 23:46