全部产品
Search
文档中心

表格存储:过滤器

更新时间:May 11, 2026

表格存储支持在查询数据时使用过滤器在服务端按指定条件进行数据过滤,本文介绍如何在 Node.js SDK 中使用过滤器。

前提条件

初始化Tablestore Client

过滤器类型

表格存储的过滤器类型包括以下 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);
    });
});

相关文档