Reputation: 611
Here I have 3 Hashes and I used sadd to define the relations
hmset user:1 name Jack
hmset category:1 name music
hmset instrument:1 name guitar
sadd user:1:category 1 sadd category:1:instrument 1
So in this case if I want to get user's instrument how should write my command? this didnt work
Sort User:1:Category:Instrument Get Category*:->Instrument
Upvotes: 1
Views: 1311
Reputation: 49942
Firstly, it is unclear from your question what it is that you're trying to do - perhaps adding more details about the data's structure and the end result that you're looking for would help. Try explaining the relationships between the user, category and instruments entities and the type of query you're looking to run.
That said, there are several problems that are apparent as is:
Note that key names are case sensitive so when calling SORT
(or any command for that matter) make sure you use key names with the same case you used to create the keys. That means the foo
, Foo
, FOO
and FoO
are all different key names.
Your SADD
statement will result in a single Set with the following contents:
127.0.0.1:6379> sadd user:1:category 1 sadd category:1:instrument 1
(integer) 3
127.0.0.1:6379> smembers user:1:category
1) "category:1:instrument"
2) "1"
3) "sadd"
To create multiple sets you'll need to issue separate SADD
commands, once for each Set.
Use Sets to express 1:N relationships, e.g. a user can have multiple instruments. To express a 1:1 relationship, simply add a field to your Hash, i.e.:
127.0.0.1:6379> hmset user:1 name Jack instrument 1
OK
127.0.0.1:6379> hgetall user:1
1) "name"
2) "Jack"
3) "instrument"
4) "1"
On the other hand, if your user can have N instruments, use a Set:
SADD user:1:instruments 1 2 3
Assuming a user has multiple instruments, getting the instruments' names is done with SORT
like so:
SORT user:1:instruments BY nosort GET instrument:*->name
Upvotes: 2