8

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.

Jand
  • 2,527
  • 12
  • 36
  • 66

1 Answers1

9

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)
thepirat000
  • 12,362
  • 4
  • 46
  • 72