1

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.

2 Answers 2

9

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

Sign up to request clarification or add additional context in comments.

7 Comments

I tried docClient.scan(params).eachPage((err, data, done) => {console.log(data_;} //// it show 2892 rows and lastEvaluatedKey Count: 2892, ScannedCount: 2892, LastEvaluatedKey: { device_id: 1451 }
You have to call done when you are done processing that page. Or you can omit the done parameter from the function e.g. (err, data) => {...} and api calls will be made immediately.
docClient.scan(params).eachPage((err, data, done) => {console.log(done); } //output: function (result) { if (result === false) return; if (response.hasNextPage()) { response.nextPage().on('complete', wrappedCallback).send(); } else { callback.call(response, null, null, AWS.util.fn.noop); } }// where and how can I use lastEvaluatedKey
try docClient.scan(params).eachPage((err, data, done) => {console.log(data); done(); }
Its working.. thank you I try,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(); });
|
4
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();
});

Comments

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.