How to search a key pattern in redis hash?

13,425

You should use HSCAN command.

For example:

redis> HMSET address_book bob_123456 Address1 mary_567894 Address2 john_123456 Address3
OK
redis> HSCAN address_book 0 match *_123456
1) "0"
2) 1) "bob_123456"
   2) "Address1"
   3) "john_123456"
   4) "Address3"

Update

Python implementation:

r = Redis(....) #redis url
for address in r.hscan_iter('address_book', match='*_123456'):
  print(address)
Share:
13,425

Related videos on Youtube

Jand
Author by

Jand

Updated on October 09, 2022

Comments

  • Jand
    Jand over 1 year

    I have a hash table whose keys are of pattern USER_TEL like:

    bob_123456  : Some address
    mary_567894 : other address
    john_123456 : third address
    

    Now, I'd like to get addresses of all uses who have the same TEL in their keys.

    What I came up with is:

    tel = 123456
    r.hmget('address_book', '*_%s' % tel)
    

    I get [None] instead of the values.