A Redis ZSET contains unique members (the keys) and scores (confined to values of floating-point numbers). The items in a ZSET are sorted and accessible by scores and the order by which the items in the ZSET were sorted. This differs from a hash because hashes can only be accessed by scores.
Begin working with this guide by opening Redis and creating a sample database. Let’s start with the first section below that shows what the Redis ZSET ZADD command can do.
Use the Redis Zadd Command :
When you want to add unique members that have different scores and store them in your Redis database, use the ZADD Redis command like this:
127.0.0.1:6379> ZADD "id" 1 "Mark"
(integer) 1
127.0.0.1:6379> ZADD "id" 2 "Arthur"
(integer) 1
127.0.0.1:6379> ZADD "id" 3 "Monica"
(integer) 1
127.0.0.1:6379> ZADD "id" 4 "Daniel"
(integer) 1
127.0.0.1:6379> ZADD "id" 5 "Sergio"
(integer) 1
Use the Redis Zrange Command :
There may be times when you’ll want to fetch just one score value.
*Use the Redis ZSET
command ZRANGE
and identify that score by its order position.
127.0.0.1:6379> ZRANGE "id" 0 0
1) "Mark"
Use the Redis Zset to Get All the Values Stored :
The ZRANGE ZSET
command returns a range of scores sorted from low to high.
Use ZRANGE
to display every score in a Redis ZSET
like this:
127.0.0.1:6379> ZRANGE "id" 0 -1
1) "Mark"
2) "Arthur"
3) "Monica"
4) "Daniel"
5) "Sergio"