Note that there are some explanatory texts on larger screens.

plurals
  1. PORedis sorted set by string values
    text
    copied!<p>Suppose you have a hash 'users' whose entries map numeric IDs to JSON-encoded arrays, so, for instance, the integer 1 maps to the string {name: 'John', surname: 'Doe', occupation: 'plumber'}.</p> <p>The numeric IDs of items in the hash are stored in various lists. Thus, if 'foobar' is one of these lists, to retrieve the actual data from it I would run a simple Lua script (implementing a server-side join operation). Or, as I've just learned, I could use something like</p> <pre><code>SORT foobar BY inexistent_key GET user:* </code></pre> <p>but that implies storing each user's data into a separate key which seems expensive (in my case I have many small collections so I want to take advantage of Redis compression of hashes).</p> <p>The question is finally this: I need to keep these lists ordered alphabetically by, say, each user's surname, then by name. What is the best way to achieve this, without changing too much the underlying data model (if possible)?</p> <p>So far the best I could think of was using the SORT command together with the BY and STORE clauses, that is, running</p> <pre><code>SORT foobar BY surname:* STORE foobar </code></pre> <p>whenever the list is changed, but that way I'd need many keys. If I could use a hash in the BY clause that would be the ideal solution, it seems to me.</p> <p>If the fields I want to sort by were somehow limited (as in, just a couple hundred surnames) I could think of mapping strings to integers and use a Redis sorted set, but this doesn't seem to be the case.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload