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

gefragt vor 2 Jahren3045 Aufrufe
2 Antworten
3
Akzeptierte Antwort

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
EXPERTE
beantwortet vor 2 Jahren
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
EXPERTE
beantwortet vor 2 Jahren
  • 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

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen