Nithya
Nithya

Reputation: 23

Dynamoose - Get table data count

How do you count as like mysql "select count(*) from tablename" , in DynamoDB using the dynamoose node module?

Upvotes: 0

Views: 2144

Answers (1)

notionquest
notionquest

Reputation: 39186

There is no direct equivalent available in DynamoDB. However, one workaround would be to get the ItemCount using describe table API.

Drawback of ItemCount:-

DynamoDB updates this value approximately every six hours. Recent changes might not be reflected in this value.

Code to get item count of Movies table from local DynamoDB instance:-

'use strict';
var dynamoose = require('dynamoose');
dynamoose.AWS.config.update({
    accessKeyId : 'AKID',
    secretAccessKey : 'SECRET',
    region : 'us-east-1'
});
dynamoose.local();

var Schema = dynamoose.Schema;
var Table = dynamoose.Table;

var table = new Table('Movies', null, null, dynamoose);

table.describe(function(err, data) {
    if (err) {
        console.log(JSON.stringify(err));

    } else {
        console.log(JSON.stringify(data, null, 2));
        console.log("Number of item =====>", JSON.stringify(data.Table.ItemCount, null, 2));
    }
});

Output:-

Number of item =====> 24

Upvotes: 2

Related Questions