3

I have a simple table with userId, pictureURL and a few other fields. I want to return all the fields with a certain userId but when I do

dynamodb.get({
  TableName: tableName,
  Key: {
    'userid': '39e1f6cb-22af-4f8c-adf5-xxxxxxxxxx'
  }
}, ...

I get The provided key element does not match the schema since it seems like it requires also the sort key. When I do

dynamodb.get({
  TableName: tableName,
  Key: {
    'userid': '39e1f6cb-22af-4f8c-adf5-xxxxxxxxxx',
    'pictureurl': '' // Or null
  }
}, ...

I get an error One or more parameter values were invalid: An AttributeValue may not contain an empty string

So how do I query for any value in the sort key?

1 Answer 1

2

With the DynamoDB DocumentClient:

  • to query for a number of items, you use query
  • to get a single item, you use get

So, use query and use the KeyConditionExpression parameter to provide a specific value for the partition key. The query operation will return all of the items from the table (or index) with that partition key value.

Here's an example:

const AWS = require("aws-sdk");
AWS.config.update({region: 'us-east-1'});

const params = {
  TableName: tableName,
  KeyConditionExpression: '#userid = :userid',
  ExpressionAttributeNames: {
    '#userid': 'userid',
  },
  ExpressionAttributeValues: {
    ':userid': '39e1f6cb-22af-4f8c-adf5-xxxxxxxxxx',
  },
};

const dc = new AWS.DynamoDB.DocumentClient();

dc.query(params, (err, data) => {
  if (err) {
    console.log('Error', err);
  } else {
    for (const item of data.Items) {
      console.log('item:', item);
    };
  }
});
Sign up to request clarification or add additional context in comments.

2 Comments

He might be referring to the AWS.DynamoDB.DocumentClient interface, which indeed exposes a get method.
I think it is essential to add that the query method only allows an equality check on the partition key, and equality, less/more, and begins_with queries on the sort key, and that's it.

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.