samson
samson

Reputation: 1617

how to get entire table data using dynamodb scan operation in node.js

Table name: users no. of rows : 100000

How can I get all records using scan operation with the help of node.js.

var params = {
    TableName:"users"};

docClient.scan(params, (error, result) => {
    if (error) {
      console.log('error', error);
    } else {
      console.log(result.Items); // x items
    }
});

but its shows part of data only. not entire table data.

Upvotes: 1

Views: 7695

Answers (2)

samson
samson

Reputation: 1617

var docClient = new AWS.DynamoDB.DocumentClient();
var params = {
    TableName: "tableName"
};
var count = 0;
docClient.scan(params).eachPage((err, data, done) => {
    if (data != null) {
        for (let index = 0; index < data.Items.length; index++) {
            const element = data.Items[index];
            count++;
            console.log("TOTAL::> " + count + " ITEM::> " + index + " DATA:: " + JSON.stringify(element));
        }
    }
    done();
});

Upvotes: 4

cementblocks
cementblocks

Reputation: 4616

You can use the eachPage function on the request.

docClient.scan(params).eachPage((err, data, done) => { ... })

You can also use the lastEvaluatedKey you get from the results of one scan as the exclusiveStartKey for the next. https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html#Query.Pagination

Upvotes: 9

Related Questions