全部產品
Search
文件中心

Tablestore:過濾器

更新時間:May 12, 2026

Table Store支援在查詢資料時使用過濾器在服務端按指定條件進行資料過濾,本文介紹如何在 Node.js SDK 中使用過濾器。

前提條件

初始化Tablestore Client

過濾器類型

Table Store的過濾器類型包括以下 2 種。

  • 單屬性列值過濾器(SingleColumnCondition):判斷單個屬性列的值是否符合條件。

  • 組合過濾器(CompositeCondition):將多個條件組合進行資料過濾。

單屬性列值過濾器

TableStore.SingleColumnCondition

參數說明

名稱

類型

說明

comparator(必選)

TableStore.ComparatorType

關係運算子,包括 EQUAL(等於)、NOT_EQUAL(不等於)、GREATER_THAN(大於)、GREATER_EQUAL(大於等於)、LESS_THAN(小於)、LESS_EQUAL(小於等於)。

columnName(必選)

string

判斷的屬性列名稱。

columnValue(必選)

STRING,INTEGER,BINARY,DOUBLE,BOOLEAN

判斷的值。

passIfMissing(可選)

boolean

行資料不包含判斷的屬性列時,是否返回該行,預設值為 true,即返回該行資料。

latestVersionOnly(可選)

boolean

是否只判斷查詢結果中最新的資料版本,預設值為 true,即當參考的屬性列存在多個資料版本時,只判斷最新的資料版本是否符合過濾條件。

範例程式碼

以下範例程式碼以範圍查詢為例查詢主索引值為 [row1, row3) 的行資料,並在查詢後進行過濾,返回 col1 屬性列的值等於 val1 的行資料。

var params = {
    tableName: 'test_table',
    // 設定查詢起始主鍵
    inclusiveStartPrimaryKey: [{ 'id': 'row1' }],
    // 設定查詢結束主鍵(返回結果不包含結束主鍵)
    exclusiveEndPrimaryKey: [{ 'id': 'row3' }]
};
// 構造過濾器,條件為 col1 == "val1"
var singleColumnCondition = new TableStore.SingleColumnCondition('col1', 'val1', TableStore.ComparatorType.EQUAL);
params.columnFilter = singleColumnCondition;

// 調用 getRange 方法查詢資料
client.getRange(params, function (err, data) {
    if (err) {
        console.log('Range get 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);
    });
});
  • 設定行資料不包含判斷的屬性列時,不返回該行。

    singleColumnCondition.passIfMissing = false;
  • 設定判斷查詢結果中所有版本的資料,此時只要有一個版本的資料符合條件,即返回該行資料。

    singleColumnCondition.latestVersionOnly = false

組合過濾器

最多支援 32 個條件的組合。

TableStore.CompositeCondition

參數說明

名稱

類型

說明

combinator(必選)

TableStore.LogicalOperator

邏輯運算子,包括 NOT(非)、AND(與)、OR(或)。

sub_conditions(必選)

Array

參與邏輯運算的過濾器,包括單屬性列值過濾器組合過濾器

範例程式碼

以下範例程式碼以範圍查詢為例查詢主索引值為 [row1, row3) 的行資料,並使用組合過濾器進行資料過濾。

var params = {
    tableName: 'test_table',
    // 設定查詢起始主鍵
    inclusiveStartPrimaryKey: [{ 'id': 'row1' }],
    // 設定查詢結束主鍵(返回結果不包含結束主鍵)
    exclusiveEndPrimaryKey: [{ 'id': 'row3' }]
};

// 構造單屬性列值過濾器1,條件為 col1 == "val1"
var singleColumnCondition1 = new TableStore.SingleColumnCondition('col1', 'val1', TableStore.ComparatorType.EQUAL);
// 構造單屬性列值過濾器2,條件為 col2 == "val2"
var singleColumnCondition2 = new TableStore.SingleColumnCondition('col2', 'val2', TableStore.ComparatorType.EQUAL);
// 構造組合過濾器1,條件為 col1 == "val1" or col2 == "val2"
var compositeCondition1 = new TableStore.CompositeCondition(TableStore.LogicalOperator.OR);
compositeCondition1.addSubCondition(singleColumnCondition1);
compositeCondition1.addSubCondition(singleColumnCondition2);
// 構造單屬性列值過濾器3,條件為 col3 == "val3"
var singleColumnCondition3 = new TableStore.SingleColumnCondition('col3', 'val3', TableStore.ComparatorType.EQUAL);
// 構造組合過濾器2,條件為 組合過濾器1 and 單屬性列值過濾器3,即 (col1 == "val1" or col2 == "val2") and col3 == "val3"
var compositeCondition2 = new TableStore.CompositeCondition(TableStore.LogicalOperator.AND);
compositeCondition2.addSubCondition(compositeCondition1);
compositeCondition2.addSubCondition(singleColumnCondition3);
// 查詢結果添加過濾器
params.columnFilter = compositeCondition2

// 調用 getRange 方法查詢資料
client.getRange(params, function (err, data) {
    if (err) {
        console.log('Range get 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);
    });
});

相關文檔