How to get all partition key vlaues?

0

Hello guys, I want to know how we can fetch all values from partition keys columns by using node js. I write one code but don't know how to pass value in ExpressionAttributeValues with :id

function getPartition(){
    var params = {
        ExpressionAttributeValues:{
            ':id': ''
        },
        FilterExpression: 'Id=:id',
        ProjectionExpression:'id',
        TableName: 'testing'
    };
    docClient.scan(params, function(err,data){
        if(err) console.log(err,err.stack);
        else console.log(data);
    })
}

with this, it returns me only the scanned count but not the items of the ID column. Where I am doing wrong, I just need to all values from the ID column which is the partition key. Thanks

asked 2 years ago2937 views
2 Answers
3
Accepted Answer

Based on your recent comment, this code will return you only the list of PK's for your table:

Using Scan you will read each item in the table. ProjectionExpression tells DynamoDB only to return attributes which match that value, in your case id.

var params = {
  TableName : 'TableName',
  ProjectionExpression: 'id'
};


documentClient.scan(params, function(err, data) {
  if (err) console.log(err);
  else console.log(data);
});
profile pictureAWS
EXPERT
answered 2 years ago
1

Its not clear what you are looking for here.

I'm going to base this on the assumption that you want to get all the attributes which is stored for a given id with id being your partition key?

You should use Query which will target a single Item Collection and be much more efficient than a Scan which reads all the items in the table.

    var params = {
        TableName: 'TableName',
        KeyConditionExpression: 'Id = :id',
        ExpressionAttributeValues: {
          ':id': '123456'
        }
      };

    documentClient.query(params, function(err, data) {
        if (err) console.log(err);
        else console.log(data);
     });

Furthermore, if you do not have a SortKey on your table, you can also use GetItem as each item will have a unique ID:

     var params = {
        TableName : 'TableName',
        Key: {
          id: '123456'
        }
      };
      
      
      documentClient.get(params, function(err, data) {
        if (err) console.log(err);
        else console.log(data);
      });
profile pictureAWS
EXPERT
answered 2 years ago
  • Thanks for your reply, Like in ExpressionAttributeValues you are passing '123456' for comparison I don't want comparison I just need to fetch all values in a list or array of ID column which is type of partition key @LeeroyHannigan

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions