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












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.









share|improve this question





























    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.









    share|improve this question



























      0












      0








      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.









      share|improve this question
















      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.






      node.js aws-lambda amazon-dynamodb






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 7:18







      adimona

















      asked Nov 18 '18 at 5:31









      adimonaadimona

      308




      308
























          1 Answer
          1






          active

          oldest

          votes


















          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.






          share|improve this answer
























          • 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 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






          • 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











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53358188%2fquery-from-a-list-of-dynamodb-items-from-an-aws-lambda-function%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          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.






          share|improve this answer
























          • 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 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






          • 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
















          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.






          share|improve this answer
























          • 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 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






          • 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














          1












          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 '18 at 21:59









          Matthew PopeMatthew Pope

          1,4421612




          1,4421612













          • 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 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






          • 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



















          • 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 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






          • 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

















          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







          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




          1





          If you already have the Partition Key and the Sort Key, then you should use a BatchGetItem request.

          – Matthew Pope
          Nov 19 '18 at 23:22





          If you already have the Partition Key and the Sort Key, then you should use a BatchGetItem 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





          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





          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




          1




          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





          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


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53358188%2fquery-from-a-list-of-dynamodb-items-from-an-aws-lambda-function%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)