How can I query by sort key in AWS DynamoDB?












0














I have a database of users, and I'd like to return all users with the last name X. I currently have "last" as the sort key for my table. I'm running the following code, but getting a "Query condition missed key schema element" error.



var params = { TableName: 'Patients',
KeyConditionExpression: '#id = :idNum',
ExpressionAttributeNames: {
'#id': 'last'
},
ExpressionAttributeValues: {
':idNum': 'Test'
}
};

docClient.query(params, function(err, data) {
if (err) {
console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
} else {
console.log("Query succeeded.");
res.json(data);
}
});









share|improve this question



























    0














    I have a database of users, and I'd like to return all users with the last name X. I currently have "last" as the sort key for my table. I'm running the following code, but getting a "Query condition missed key schema element" error.



    var params = { TableName: 'Patients',
    KeyConditionExpression: '#id = :idNum',
    ExpressionAttributeNames: {
    '#id': 'last'
    },
    ExpressionAttributeValues: {
    ':idNum': 'Test'
    }
    };

    docClient.query(params, function(err, data) {
    if (err) {
    console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
    } else {
    console.log("Query succeeded.");
    res.json(data);
    }
    });









    share|improve this question

























      0












      0








      0







      I have a database of users, and I'd like to return all users with the last name X. I currently have "last" as the sort key for my table. I'm running the following code, but getting a "Query condition missed key schema element" error.



      var params = { TableName: 'Patients',
      KeyConditionExpression: '#id = :idNum',
      ExpressionAttributeNames: {
      '#id': 'last'
      },
      ExpressionAttributeValues: {
      ':idNum': 'Test'
      }
      };

      docClient.query(params, function(err, data) {
      if (err) {
      console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
      } else {
      console.log("Query succeeded.");
      res.json(data);
      }
      });









      share|improve this question













      I have a database of users, and I'd like to return all users with the last name X. I currently have "last" as the sort key for my table. I'm running the following code, but getting a "Query condition missed key schema element" error.



      var params = { TableName: 'Patients',
      KeyConditionExpression: '#id = :idNum',
      ExpressionAttributeNames: {
      '#id': 'last'
      },
      ExpressionAttributeValues: {
      ':idNum': 'Test'
      }
      };

      docClient.query(params, function(err, data) {
      if (err) {
      console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
      } else {
      console.log("Query succeeded.");
      res.json(data);
      }
      });






      javascript node.js amazon-web-services amazon-dynamodb dynamodb-queries






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 22:55









      Joe Smith

      94




      94
























          1 Answer
          1






          active

          oldest

          votes


















          1














          To query you must provide a partition key, so with your table, as it is, your only option would be to do a Scan (which is expensive and almost never recommended).



          However you can easily add Global Secondary Indexes to allow you to use another key as your partition.



          In this case you can add a GSI with last as your partition key.



          Then you would be able to Query the global secondary index (note the IndexName parameter) by last name.



          If you want to go the scan route, however, you'll need to use:



          docClient.scan({
          TableName: 'Patients',
          FilterExpression: '#l = :l',
          ExpressionAttributeNames: { '#l': 'last' },
          ExpressionAttributeValues: { ':l': 'Test' },
          Limit: 1
          }, (err, data) => { /* callback */ })


          Just remember that using scans can get expensive and put a strain on your table quickly (more detail here)






          share|improve this answer























          • Thanks for the response! If I decided to go the route of doing a scan instead, how would I modify my code?
            – Joe Smith
            Nov 12 at 19:01










          • No worries- I've added an example- although you really should avoid frequent scans on a dynamo db table if you possibly can.
            – thomasmichaelwallace
            Nov 13 at 8:24










          • Awesome, thank you. I'll take a look at the global index option as well.
            – Joe Smith
            Nov 14 at 1:51










          • Good luck! Don't forget to mark the question answered!
            – thomasmichaelwallace
            Nov 14 at 7:25






          • 1




            FYI, a GSI does not require the primary key to be unique, so you don't need to provide a sort key for a GSI. (Though it never hurts to add one.) docs.aws.amazon.com/amazondynamodb/latest/developerguide/…
            – Matthew Pope
            Nov 26 at 0:11











          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%2f53254060%2fhow-can-i-query-by-sort-key-in-aws-dynamodb%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














          To query you must provide a partition key, so with your table, as it is, your only option would be to do a Scan (which is expensive and almost never recommended).



          However you can easily add Global Secondary Indexes to allow you to use another key as your partition.



          In this case you can add a GSI with last as your partition key.



          Then you would be able to Query the global secondary index (note the IndexName parameter) by last name.



          If you want to go the scan route, however, you'll need to use:



          docClient.scan({
          TableName: 'Patients',
          FilterExpression: '#l = :l',
          ExpressionAttributeNames: { '#l': 'last' },
          ExpressionAttributeValues: { ':l': 'Test' },
          Limit: 1
          }, (err, data) => { /* callback */ })


          Just remember that using scans can get expensive and put a strain on your table quickly (more detail here)






          share|improve this answer























          • Thanks for the response! If I decided to go the route of doing a scan instead, how would I modify my code?
            – Joe Smith
            Nov 12 at 19:01










          • No worries- I've added an example- although you really should avoid frequent scans on a dynamo db table if you possibly can.
            – thomasmichaelwallace
            Nov 13 at 8:24










          • Awesome, thank you. I'll take a look at the global index option as well.
            – Joe Smith
            Nov 14 at 1:51










          • Good luck! Don't forget to mark the question answered!
            – thomasmichaelwallace
            Nov 14 at 7:25






          • 1




            FYI, a GSI does not require the primary key to be unique, so you don't need to provide a sort key for a GSI. (Though it never hurts to add one.) docs.aws.amazon.com/amazondynamodb/latest/developerguide/…
            – Matthew Pope
            Nov 26 at 0:11
















          1














          To query you must provide a partition key, so with your table, as it is, your only option would be to do a Scan (which is expensive and almost never recommended).



          However you can easily add Global Secondary Indexes to allow you to use another key as your partition.



          In this case you can add a GSI with last as your partition key.



          Then you would be able to Query the global secondary index (note the IndexName parameter) by last name.



          If you want to go the scan route, however, you'll need to use:



          docClient.scan({
          TableName: 'Patients',
          FilterExpression: '#l = :l',
          ExpressionAttributeNames: { '#l': 'last' },
          ExpressionAttributeValues: { ':l': 'Test' },
          Limit: 1
          }, (err, data) => { /* callback */ })


          Just remember that using scans can get expensive and put a strain on your table quickly (more detail here)






          share|improve this answer























          • Thanks for the response! If I decided to go the route of doing a scan instead, how would I modify my code?
            – Joe Smith
            Nov 12 at 19:01










          • No worries- I've added an example- although you really should avoid frequent scans on a dynamo db table if you possibly can.
            – thomasmichaelwallace
            Nov 13 at 8:24










          • Awesome, thank you. I'll take a look at the global index option as well.
            – Joe Smith
            Nov 14 at 1:51










          • Good luck! Don't forget to mark the question answered!
            – thomasmichaelwallace
            Nov 14 at 7:25






          • 1




            FYI, a GSI does not require the primary key to be unique, so you don't need to provide a sort key for a GSI. (Though it never hurts to add one.) docs.aws.amazon.com/amazondynamodb/latest/developerguide/…
            – Matthew Pope
            Nov 26 at 0:11














          1












          1








          1






          To query you must provide a partition key, so with your table, as it is, your only option would be to do a Scan (which is expensive and almost never recommended).



          However you can easily add Global Secondary Indexes to allow you to use another key as your partition.



          In this case you can add a GSI with last as your partition key.



          Then you would be able to Query the global secondary index (note the IndexName parameter) by last name.



          If you want to go the scan route, however, you'll need to use:



          docClient.scan({
          TableName: 'Patients',
          FilterExpression: '#l = :l',
          ExpressionAttributeNames: { '#l': 'last' },
          ExpressionAttributeValues: { ':l': 'Test' },
          Limit: 1
          }, (err, data) => { /* callback */ })


          Just remember that using scans can get expensive and put a strain on your table quickly (more detail here)






          share|improve this answer














          To query you must provide a partition key, so with your table, as it is, your only option would be to do a Scan (which is expensive and almost never recommended).



          However you can easily add Global Secondary Indexes to allow you to use another key as your partition.



          In this case you can add a GSI with last as your partition key.



          Then you would be able to Query the global secondary index (note the IndexName parameter) by last name.



          If you want to go the scan route, however, you'll need to use:



          docClient.scan({
          TableName: 'Patients',
          FilterExpression: '#l = :l',
          ExpressionAttributeNames: { '#l': 'last' },
          ExpressionAttributeValues: { ':l': 'Test' },
          Limit: 1
          }, (err, data) => { /* callback */ })


          Just remember that using scans can get expensive and put a strain on your table quickly (more detail here)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 at 9:21

























          answered Nov 12 at 9:08









          thomasmichaelwallace

          2,5251817




          2,5251817












          • Thanks for the response! If I decided to go the route of doing a scan instead, how would I modify my code?
            – Joe Smith
            Nov 12 at 19:01










          • No worries- I've added an example- although you really should avoid frequent scans on a dynamo db table if you possibly can.
            – thomasmichaelwallace
            Nov 13 at 8:24










          • Awesome, thank you. I'll take a look at the global index option as well.
            – Joe Smith
            Nov 14 at 1:51










          • Good luck! Don't forget to mark the question answered!
            – thomasmichaelwallace
            Nov 14 at 7:25






          • 1




            FYI, a GSI does not require the primary key to be unique, so you don't need to provide a sort key for a GSI. (Though it never hurts to add one.) docs.aws.amazon.com/amazondynamodb/latest/developerguide/…
            – Matthew Pope
            Nov 26 at 0:11


















          • Thanks for the response! If I decided to go the route of doing a scan instead, how would I modify my code?
            – Joe Smith
            Nov 12 at 19:01










          • No worries- I've added an example- although you really should avoid frequent scans on a dynamo db table if you possibly can.
            – thomasmichaelwallace
            Nov 13 at 8:24










          • Awesome, thank you. I'll take a look at the global index option as well.
            – Joe Smith
            Nov 14 at 1:51










          • Good luck! Don't forget to mark the question answered!
            – thomasmichaelwallace
            Nov 14 at 7:25






          • 1




            FYI, a GSI does not require the primary key to be unique, so you don't need to provide a sort key for a GSI. (Though it never hurts to add one.) docs.aws.amazon.com/amazondynamodb/latest/developerguide/…
            – Matthew Pope
            Nov 26 at 0:11
















          Thanks for the response! If I decided to go the route of doing a scan instead, how would I modify my code?
          – Joe Smith
          Nov 12 at 19:01




          Thanks for the response! If I decided to go the route of doing a scan instead, how would I modify my code?
          – Joe Smith
          Nov 12 at 19:01












          No worries- I've added an example- although you really should avoid frequent scans on a dynamo db table if you possibly can.
          – thomasmichaelwallace
          Nov 13 at 8:24




          No worries- I've added an example- although you really should avoid frequent scans on a dynamo db table if you possibly can.
          – thomasmichaelwallace
          Nov 13 at 8:24












          Awesome, thank you. I'll take a look at the global index option as well.
          – Joe Smith
          Nov 14 at 1:51




          Awesome, thank you. I'll take a look at the global index option as well.
          – Joe Smith
          Nov 14 at 1:51












          Good luck! Don't forget to mark the question answered!
          – thomasmichaelwallace
          Nov 14 at 7:25




          Good luck! Don't forget to mark the question answered!
          – thomasmichaelwallace
          Nov 14 at 7:25




          1




          1




          FYI, a GSI does not require the primary key to be unique, so you don't need to provide a sort key for a GSI. (Though it never hurts to add one.) docs.aws.amazon.com/amazondynamodb/latest/developerguide/…
          – Matthew Pope
          Nov 26 at 0:11




          FYI, a GSI does not require the primary key to be unique, so you don't need to provide a sort key for a GSI. (Though it never hurts to add one.) docs.aws.amazon.com/amazondynamodb/latest/developerguide/…
          – Matthew Pope
          Nov 26 at 0:11


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53254060%2fhow-can-i-query-by-sort-key-in-aws-dynamodb%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

          Full-time equivalent

          Bicuculline

          さくらももこ