Etika49
Etika49

Reputation: 752

DynamoDB query without sort key

I have a DynamoDB table that contains primary key : userID, sort key : sessionID and another column which is named examID.

i would like to return a list that returns all records that have the userID and examID sent by me. Here is my code:

export const main = handler(async (event, context) => {

  const data = JSON.parse(event.body);
  const params = {
    TableName: process.env.tableNameExamResults,
    KeyConditionExpression: "userId = :userId and examId = :examId",
    ExpressionAttributeValues: {
      ":userId": event.requestContext.identity.cognitoIdentityId,
      ":examId": data.examId

    }
  };

  const result = await dynamoDb.query(params);

  
  return result.Items;
});


this is the error that i get: { "statusCode": 500, "body": "{"error":"Query condition missed key schema element: sessionId"}",

...

I think that maybe i should include a Filter Expression or not sure if i have to recreate the DB with an index.

Any help is appreciated. thank you

Upvotes: 4

Views: 4140

Answers (1)

Etika49
Etika49

Reputation: 752

i just added a new GSI and then i also edited my code :

const params = {
    TableName: process.env.tableNameExamResults,
    IndexName: 'userId-examId-index', 
    KeyConditionExpression: "userId = :userId and examId = :examId",
    ExpressionAttributeValues: {
      ":userId": event.requestContext.identity.cognitoIdentityId,
      ":examId": data.examId

    }
  };

This seems to work now.

Upvotes: 1

Related Questions