hscan
1. 基本語法
hscan(hkey
, cursor
, ['MATCH', pattern]
, ['COUNT', count]
,callback
)
hkey
, cursor
, ['MATCH', pattern]
, ['COUNT', count]
,callback
) 針對hkey
資料進行漸進式的遍歷(Traversal)取值,透過MATCH
參數篩選欄位(Sub-Key)資料,該指令不保證回傳的鍵值對(Sub-Key : Value)數量,但可透過COUNT
制定最少的回傳數目,並透過新的cursor
來取得下一群資料內容,當cursor
等於'0'時,代表遍歷已結束。
Parameter
Default
pattern
'*'
count
'10'
2. 範例
(1) 預設參數會試圖取得所有hkey
資料
hkey
資料若hkey
對應欄位(Sub-Key)過多,則先回傳部分資料及非零cursor,並得以透過新cursor取得下一組hkey
資料
const dict = {
name: 'John Miller',
gender: 'M',
age: '34',
hobby1 : 'Whistle',
hobby2 : 'War',
hobby3 : 'Walk'
};
client.hmset('user:5', dict);
let cursor = '0';
client.hscan('user:5', cursor , (err, scanner) => {
cursor = scanner[0];
let kv_list = scanner[1];
console.log('Cursor:', cursor);
console.log('Data:', kv_list);
});
Cursor: 0
Data: [
'name', 'John Miller',
'gender', 'M',
'age', '34',
'hobby1', 'Whistle',
'hobby2', 'War',
'hobby3', 'Walk'
]
(2) 模糊篩選subkey
let cursor = '0';
client.hscan('user:5', cursor, 'MATCH', 'hobby*', 'COUNT', '10',
(err, scanner) => {
let cursor = scanner[0];
let kv_list = scanner[1];
console.log('Next Cursor:', cursor);
console.log('Cuurent Data:', kv_list);
}
);
Cursor: 0
Data: [ 'hobby1', 'Whistle', 'hobby2', 'War', 'hobby3', 'Walk' ]
Last updated
Was this helpful?