Skip to content

Instantly share code, notes, and snippets.

@indyarocks
Created May 21, 2017 19:37
Show Gist options
  • Save indyarocks/6532742191a1a9daa9d5c073812ca1ab to your computer and use it in GitHub Desktop.
Save indyarocks/6532742191a1a9daa9d5c073812ca1ab to your computer and use it in GitHub Desktop.
Redis ZSET Data type
127.0.0.1:6379> ZADD class1:math 98 Tithi 50 Raju 80 Andrew # Create a new class 1 math score SET.
(integer) 3 # Add three members with their respective scores
127.0.0.1:6379> ZRANGEBYSCORE class1:math 0 10 # Fetch students scored between 0 to 10. No Student scored between 0 to 10
(empty list or set)
127.0.0.1:6379> ZRANGEBYSCORE class1:math 0 60 # Fetch students scored between 0 to 60. Raju
1) "Raju"
127.0.0.1:6379> ZRANGEBYSCORE class1:math 0 60 WITHSCORES # Fetch students with scores who scored between 0 to 60. Raju
1) "Raju"
2) "50"
127.0.0.1:6379> ZRANGE class1:math 0 10 WITHSCORES # Fetch students from ZSET class1:math with position 0 to 10.
1) "Raju" # Returns all students as only 3 students are added to ZSET class1:math
2) "50"
3) "Andrew"
4) "80"
5) "Tithi"
6) "98"
127.0.0.1:6379> ZRANGE class1:math 0 1 WITHSCORES # Fetch students from ZSET class1:math with position 0 to 1.
1) "Andrew" # Returns first 2 students from ZSET class1:math
2) "80"
3) "Tithi"
4) "98"
127.0.0.1:6379> ZRANGE class1:math 0 -1 WITHSCORES # Fetch ALL students from ZSET class1:math with position 0 to -1(end).
1) "Raju"
2) "50"
3) "Andrew"
4) "80"
5) "Tithi"
6) "98"
127.0.0.1:6379> ZREM class1:math Arjun # REMOVE student Arjun from ZSET class1:math. Returns 0 as failure
(integer) 0
127.0.0.1:6379> ZRANGE class1:math 0 -1 WITHSCORES # Fetch ALL students from ZSET class1:math with position 0 to -1(end). Same list as earlier
1) "Raju"
2) "50"
3) "Andrew"
4) "80"
5) "Tithi"
6) "98"
127.0.0.1:6379> ZREM class1:math Raju # REMOVE student Raju from ZSET class1:math. Returns 1 as success
(integer) 1
127.0.0.1:6379> ZRANGE class1:math 0 -1 WITHSCORES # Fetch ALL students from ZSET class1:math with position 0 to -1(end).
1) "Andrew" # NOTE: Raju is not listed in complete list
2) "80"
3) "Tithi"
4) "98"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment