Tablestore supports server-side filtering to return only the data that matches your specified conditions. This topic explains how to use filters with the Node.js SDK.
Prerequisites
Filter types
Tablestore provides two types of filters:
SingleColumnCondition: Filters data based on the value of a single attribute column.
CompositeCondition: Combines multiple filter conditions.
SingleColumnCondition
TableStore.SingleColumnConditionExample
The following example performs a range query for primary key values within the range of [row1, row3). It then applies a filter to return only rows where the value of the col1 attribute column is val1.
var params = {
tableName: 'test_table',
// Set the start primary key for the query.
inclusiveStartPrimaryKey: [{ 'id': 'row1' }],
// Set the end primary key for the query (exclusive).
exclusiveEndPrimaryKey: [{ 'id': 'row3' }]
};
// Construct a filter with the condition: col1 == "val1".
var singleColumnCondition = new TableStore.SingleColumnCondition('col1', 'val1', TableStore.ComparatorType.EQUAL);
params.columnFilter = singleColumnCondition;
// Call the getRange method to query data.
client.getRange(params, function (err, data) {
if (err) {
console.log('Range get failed with error: ', err);
return;
}
// Process the response.
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);
});
});To exclude rows that do not contain the specified attribute column, set passIfMissing to false.
singleColumnCondition.passIfMissing = false;To evaluate all data versions, set latestVersionOnly to false. If any data version meets the condition, the row is returned.
singleColumnCondition.latestVersionOnly = false
CompositeCondition
You can combine up to 32 conditions.
TableStore.CompositeConditionExample
The following example performs a range query for rows with primary key values in the range of [row1, row3) and then applies a composite filter.
var params = {
tableName: 'test_table',
// Set the start primary key.
inclusiveStartPrimaryKey: [{ 'id': 'row1' }],
// Set the end primary key (exclusive).
exclusiveEndPrimaryKey: [{ 'id': 'row3' }]
};
// Construct the first single column condition: col1 == "val1".
var singleColumnCondition1 = new TableStore.SingleColumnCondition('col1', 'val1', TableStore.ComparatorType.EQUAL);
// Construct the second single column condition: col2 == "val2".
var singleColumnCondition2 = new TableStore.SingleColumnCondition('col2', 'val2', TableStore.ComparatorType.EQUAL);
// Construct the first composite condition: col1 == "val1" OR col2 == "val2".
var compositeCondition1 = new TableStore.CompositeCondition(TableStore.LogicalOperator.OR);
compositeCondition1.addSubCondition(singleColumnCondition1);
compositeCondition1.addSubCondition(singleColumnCondition2);
// Construct the third single column condition: col3 == "val3".
var singleColumnCondition3 = new TableStore.SingleColumnCondition('col3', 'val3', TableStore.ComparatorType.EQUAL);
// Construct the second composite condition: (col1 == "val1" OR col2 == "val2") AND col3 == "val3".
var compositeCondition2 = new TableStore.CompositeCondition(TableStore.LogicalOperator.AND);
compositeCondition2.addSubCondition(compositeCondition1);
compositeCondition2.addSubCondition(singleColumnCondition3);
// Add the filter to the query.
params.columnFilter = compositeCondition2
// Call the getRange method to query data.
client.getRange(params, function (err, data) {
if (err) {
console.log('Range get failed with error: ', err);
return;
}
// Process the response.
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);
});
});