嵌套键的Redis列表 [英] Redis list of nested keys
问题描述
我已将以下格式的列表保存在Redis数据库中.
I have saved lists in following format in my Redis database.
键:内键1:内键2:内键3
key:inner-key1:inner-key2:inner-key3
例如,我的数据库如下所示:
For example my database looks like this:
A:B:X:val1
A:B:X:val1
A:B:Y:val2
A:B:Y:val2
A:C:X:val3
A:C:X:val3
A:C:Y:val4
A:C:Y:val4
如何获取密钥B的内部密钥? 我正在尝试使用 按键A:B:*, 但是其结果是整行"A:B:X:val1"和"A:B:X:val2". 我所需要的只是获得第一个"A:B"内键.格式为[X,Y].
How can I get inner keys for key B? I was trying to get it using KEYS A:B:*, but result of this are whole lines "A:B:X:val1" and "A:B:X:val2". All I need is to get only first inner key of "A:B" in format for example [X, Y].
推荐答案
您可以使用 Redis哈希以达到相同的效果:
You can use Redis Hash to acheive the same:
您的密钥是
A:B:X:val1
A:B:Y:val2
A:C:X:val3
A:C:Y:val4
您可以将密钥另存为
HSET A:B X val1
HSET A:B Y val2
HSET A:C X val1
HSET A:C Y val2
现在您可以按需获取A:B的所有键
Now to get all keys for A:B you can do like
HKEYS A:B this will return [X Y]
这篇关于嵌套键的Redis列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!