2

I have the following structure in Redis,

commKey:hkey1
    target1 - value1
    target2 - value2
commKey:hkey2
    target2 - value3
    target3 - value4
commKey:hkey3
    target1 - value5
    target3 - value6
...

commkey will appear on all hashkeys followed by a namespace and a unique string(hkey1, hkey2, hkey3...). I want to remove all target1 s in all keys. target1 appears in commKey:hkey1 and commKey:hkey3. So, this is something like match commKey:* and del target1. I tried to do it with hscan which enables pattern match on subkeys(fields). But I need to do it on "haskkey". How to do it in an efficient way? Thanks in advance.

PS: We have commKey because in future we may want to add another type of hashkey which don't start with commKey and it should not be affected by this.

RaR
  • 3,075
  • 3
  • 23
  • 48

1 Answers1

0

You should scan the keys, not the hash.

  1. Use SCAN command to get keys that match commKey:*
  2. For each key, call HDEL to remove the given target, no matter whether the hash has such a field or not.
for_stack
  • 21,012
  • 4
  • 35
  • 48
  • That's a possible way. we need to do two operations. `SCAN` and `HDEL`. Isn't it possible with single operation? Is there any way to do combine both match and delete – RaR Dec 02 '16 at 12:22
  • 1
    @RaR AFAIK, there's no such way... If you find one, please share it with me :) – for_stack Dec 02 '16 at 12:26
  • 1
    Later I found that the actual need is to do scan as well as hscan. `SCAN` with match `commKey:*` and for each key returned, have to do `HSCAN` with match `target1*`. Then for each result have to perform `HDEL`. To avoid these many steps, I was looking for as easier and minimal way – RaR Dec 02 '16 at 12:30