All Products
Search
Document Center

Tablestore:Read data in a range

Last Updated:Jun 22, 2026

Use the `getRange` method in the Tablestore Node.js SDK to read data within a specified primary key range.

Prerequisites

Initialize a Tablestore client

Method

getRange: function getRange(params, callback)

Parameters

Name

Type

Description

tableName (Required)

string

The name of the data table.

inclusiveStartPrimaryKey (Required)

Array

The start primary key, including the primary key column name and value.

  • The returned data includes the start primary key.

  • The number and types of primary keys must be the same as those of the data table.

  • When you read data in forward order, the start primary key must be smaller than the end primary key.

  • When you read data in reverse order, the start primary key must be greater than the end primary key.

  • TableStore.INF_MIN represents negative infinity, and TableStore.INF_MAX represents positive infinity.

exclusiveEndPrimaryKey (Required)

Array

The end primary key, including the primary key column name and value.

  • The returned data does not include the end primary key.

  • The number and types of primary keys must be the same as those of the data table.

  • TableStore.INF_MIN represents negative infinity, and TableStore.INF_MAX represents positive infinity.

direction (Optional)

string

The read direction.

  • TableStore.Direction.FORWARD (default): Reads data in forward order.

  • TableStore.Direction.BACKWARD: Reads data in reverse order.

maxVersions (Optional)

number

The maximum number of versions to return. The default value is 1.

  • If more versions meet the filter condition than the specified maximum, results are returned in descending order of timestamp.

timeRange (Optional)

object

The version range of the data.

  • Attribute columns in a Tablestore table can have multiple versions. If you specify a version range, only data within that range is returned.

limit (Optional)

number

The maximum number of rows to return in a single request. Must be greater than 0. If matching rows exceed this value, the response includes the specified number of rows and the start primary key for the next query.

columnsToGet (Optional)

Array

The columns to read. Can be primary key columns or attribute columns.

  • If not specified, the entire row is returned.

  • If the retrieved row does not contain any of the specified columns, null is returned.

columnFilter (Optional)

TableStore.ColumnCondition

The filter to apply. For more information, see Filters.

  • If you set both columnsToGet and columnFilter, the system first selects rows based on columnsToGet and then filters the data based on columnFilter.

transactionId (Optional)

string

The ID that uniquely identifies a local transaction. For more information, see Local transactions.

Examples

The following example reads data from the test_table table where the primary key value is greater than row1.

var params = {
    tableName: 'test_table',
    // Set the start primary key for the query.
    inclusiveStartPrimaryKey: [{ 'id': 'row1' }],
    // Set the end primary key for the query. The end primary key is not included in the result.
    exclusiveEndPrimaryKey: [{ 'id': TableStore.INF_MAX }]
};

// Call the getRange method to query data.
client.getRange(params, function (err, data) {
    if (err) {
        console.log('Get range failed with error: ', err);
        return;
    }

    console.log('* RequestId: ', data.RequestId);
    console.log('* Read CU Cost: ', data.consumed.capacityUnit.read);
    console.log('* Write CU Cost: ', data.consumed.capacityUnit.write);
    console.log('* Rows Data: ');
    data.rows.forEach(function (row) {
         console.log(row);
    });
});

A single range scan returns a maximum of 5,000 rows or 4 MB of data. If this limit is exceeded, the response includes the start primary key for the next read. Use the following code to perform iterative queries.

async function getRangeSample() {
    try {
        while (true) {
            // Call the getRange method to query data.
            const data = await client.getRange(params);

            // Process the returned result.
            console.log('* RequestId: ', data.RequestId);
            console.log('* Read CU Cost: ', data.consumed.capacityUnit.read);
            console.log('* Write CU Cost: ', data.consumed.capacityUnit.write);
            console.log('* Rows Data: ');
            data.rows.forEach(function (row) {
                console.log(row);
            });

            // Set the start primary key for the next read.
            if (data.nextStartPrimaryKey) {
                params.inclusiveStartPrimaryKey = data.nextStartPrimaryKey.map(item => {
                    return {
                        [item.name]: item.value
                    };
                });
            } else {
                break;
            }
        }
    } catch (err) {
        console.log('Range get failed with error: ', err);
    }
}

getRangeSample();

You can also configure the following settings when querying data.

  • Set the data read direction.

    var params = {
        tableName: 'test_table',
        // Set the start primary key for the query. When you read data in reverse order, the start primary key must be greater than the end primary key.
        inclusiveStartPrimaryKey: [{ 'id': TableStore.INF_MAX }],
        // Set the end primary key for the query. The end primary key is not included in the result.
        exclusiveEndPrimaryKey: [{ 'id': 'row1' }],
        // Set the read direction to reverse.
        direction: TableStore.Direction.BACKWARD
    };
  • Set a version range. Only data within the specified range is returned.

    // Set the version range for the query to the last 24 hours.
    params.timeRange = {
        startTime: (Date.now() - 86400 * 1000).toString(),
        endTime: Date.now().toString()
    }
  • Specify the attribute columns to read.

    params.columnsToGet = ['col2']
  • Set the maximum number of rows to return in a single request.

    params.limit = 10

References

Batch Read Data