16

Say I add two keys:

SET husband Bob
SET wife Alice

Then add these to a set:

SADD family husband wife

I can get the keys of this set with SMEMBERS family, which will return:

1) "wife"
2) "husband"

What I really want is the values:

1) "Alice"
2) "Bob"

Is this possible, in one operation? Essentially, I want to pipeline SMEMBERS with MGET.

2 Answers 2

21

SMEMBERS, but if the Set is big enough your database will take time to return all the members, during which it will be blocked. In such cases the use of SSCAN is recommended.

EDIT: missed the question itself :) use SORT family BY nosort GET *

2
  • 1
    SMEMBERS returns the keys. I want the values.
    – Deane
    Commented Jan 22, 2016 at 21:21
  • Could you share a reference please on how SORT family BY nosort GET * works, complexity etc?
    – jitendra
    Commented Sep 6, 2018 at 17:49
3

The first thing you need to understand is Redis doesn't allow you to put a set in a set - no nested data structures. Likewise they are no vets and values in a set - only members which are strings in your case. This is why you can't "get the values".

It really sounds like the structure you are wanting is not a set but a hash. Using a hash will allow you do do exactly what you ask for.

hset family husband bob hset family wife alice

And then to get what you call the values use hvals family.

This is the proper way to do it as it uses the right data structure, matches your terminology to the structure, and provides exaxtly what you want led without the performance penalties of sort. Further it allows you to use the various hash commands to select or update specific members of the family.

You could also use hgetall to use the mapping. That way your code knows Alice is the wife rather than the daughter.

Overall hashes are certainly the way to go here for virtually every reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.