1

I need to get count of records in my DynamoDB table using Node.js?

Simply, How to write Node.js snippet for same?

aws dynamodb scan --table-name dev-xxx-table --select "COUNT"

Tried https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#query-property

const params = {
    TableName: Table(),
    Select: COUNT
  }

  try {
    const count = await dynamoDb.query(params).promise()

but got

"ReferenceError: COUNT is not defined",

I couldn't find anything in the documentation for the same(or finding aggregation for DynamoDB table). Any lead will be appreciated.

1
  • 1
    Nodejs sdk has scan. So what exactly is the issue? Commented Aug 17, 2021 at 5:58

1 Answer 1

3

Seems to me like there's two errors in the code, first you're using the wrong function (query instead of scan) and second, you're not passing COUNT as a String, but as a variable, which is why you're getting the error. Try the following code (untested):

const params = {
  TableName: Table(),
  Select: "COUNT",
};
const count = await dynamoDb.scan(params).promise();

That being said, be aware that this is a very expensive and probably slow operation that you do not want to be doing on a very large dataset.

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

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.