A hash data structure is a collection of key-value pairs. It is similar to a hash table in other programming languages. The hash data structure is often used to implement other data structures like queues and stacks.
This command sets the specified field in the hash to the specified value.
hset user:1 name "subham"
hset user:1 email "[email protected]"
Visualize the hash in the Redis Commander.
Field | Value |
---|---|
name | subham |
codexam |
This command returns the value of the specified field in the hash.
hset user:1 name "subham"
hset user:1 email "
hget user:1 name
"subham"
This command returns all the fields and values of the hash.
hset user:1 name "subham"
hset user:1 email "[email protected]"
hgetall user:1
1) "name"
2) "subham"
3) "email"
4) "[email protected]"
This command deletes the specified field from the hash.
hset user:1 name "subham"
hset user:1 email "[email protected]"
hdel user:1 email
(integer) 1
hgetall user:1
1) "name"
2) "subham"
This command checks if the specified field exists in the hash.
hset user:1 name "subham"
hset user:1 role "admin"
hexists user:1 role
(integer) 1
This command returns all the fields of the hash.
hset user:1 name "subham"
hset user:1 role "admin"
hkeys user:1
1) "name"
2) "role"
This command returns all the values of the hash.
hset user:1 name "subham"
hset user:1 role "admin"
hvals user:1
1) "subham"
2) "admin"
This command returns the number of fields in the hash.
hset user:1 name "subham"
hset user:1 role "admin"
hlen user:1
(integer) 2
This command increments the specified field by the specified value.
hset user:1 age 20
hincrby user:1 age 5
(integer) 25
This command increments the specified field by the specified value.
hset user:1 age 20
hincrbyfloat user:1 age 5.5
"25.5"
This command sets the specified fields to their respective values in the hash.
hmset user:1 name "subham" role "admin"
hgetall user:1
1) "name"
2) "subham"
3) "role"
4) "admin"
This command returns the values of the specified fields in the hash.
hmset user:1 name "subham" role "admin"
hmget user:1 name role
1) "subham"
2) "admin"
This command sets the specified field to the specified value if the field does not exist in the hash.
hsetnx user:1 name "subham"
hsetnx user:1 name "codexam"
(integer) 0
This command returns the length of the value of the specified field in the hash.
hset user:1 name "subham"
hstrlen user:1 name
(integer) 6
This command scans the hash for fields matching the specified pattern.
hset user:1 name "subham"
hset user:1 role "admin"
hscan user:1 0 match "na*"
1) "0"
2) 1) "name"
2) "subham"
A sorted set data structure is a collection of unique elements sorted by a score. It is similar to a sorted list in other programming languages. Priority queues are often implemented using a sorted set data structure.
Here are some operations performed on a sorted set:
Operation | Element | Score |
---|---|---|
Add | 1 | 1 |
Add | 2 | 2 |
Add | 3 | 3 |
Remove | 3 | 3 |
Remove | 2 | 2 |
Remove | 1 | 1 |
- Sorted Set Data Structure
- 5.1. ZADD
- 5.2. ZREM
- 5.3. ZSCORE
- 5.4. ZRANGE
- 5.5. ZREVRANGE
- 5.6. ZRANGEBYSCORE
- 5.7. ZREVRANGEBYSCORE
- 5.8. ZCARD
- 5.9. ZCOUNT
- 5.10. ZINCRBY
- 5.11. ZLEXCOUNT
- 5.12. ZRANGEBYLEX
- 5.13. ZREVRANGEBYLEX
- 5.14. ZREMRANGEBYLEX
- 5.15. ZREMRANGEBYRANK
- 5.16. ZREMRANGEBYSCORE
- 5.17. ZUNIONSTORE
- 5.18. ZINTERSTORE
- 5.19. ZSCAN
- 5.20. ZRANK
This command adds the specified member to the sorted set with the specified score.
zadd user:1 1 "subham"
zadd user:1 2 "codexam"
zadd user:1 3 "xamcodexam"
zrange user:1 0 -1 withscores
1) "subham"
2) "1"
3) "codexam"
4) "2"
5) "xamcodexam"
6) "3"
Visualize the sorted set in the Redis Commander.
Index | Element | Score |
---|---|---|
0 | subham | 1 |
1 | codexam | 2 |
2 | xamcodexam | 3 |
This command removes the specified member from the sorted set.
zadd user:1 1 "subham"
zadd user:1 2 "codexam"
zadd user:1 3 "xamcodexam"
zrem user:1 "subham"
zrange user:1 0 -1 withscores
1) "codexam"
2) "2"
3) "xamcodexam"
4) "3"
This command returns the score of the specified member in the sorted set.
zadd user:1 1 "subham"
zadd user:1 2 "codexam"
zadd user:1 3 "xamcodexam"
zscore user:1 "subham"
"1"
This command returns the specified range of elements from the sorted set.
zadd user:1 1 "subham"
zadd user:1 2 "codexam"
zadd user:1 3 "xamcodexam"
zrange user:1 0 -1 withscores
1) "subham"
2) "1"
3) "codexam"
4) "2"
5) "xamcodexam"
6) "3"
This command returns the specified range of elements from the sorted set in reverse order.
zadd user:1 1 "subham"
zadd user:1 2 "codexam"
zadd user:1 3 "xamcodexam"
zrevrange user:1 0 -1 withscores
1) "xamcodexam"
2) "3"
3) "codexam"
4) "2"
5) "subham"
6) "1"
This command returns the specified range of elements from the sorted set by score.
zadd user:1 1 "subham"
zadd user:1 2 "codexam"
zadd user:1 3 "xamcodexam"
zrangebyscore user:1 1 2 withscores
1) "subham"
2) "1"
3) "codexam"
4) "2"
This command returns the specified range of elements from the sorted set by score in reverse order.
zadd user:1 1 "subham"
zadd user:1 2 "codexam"
zadd user:1 3 "xamcodexam"
zrevrangebyscore user:1 2 1 withscores
1) "codexam"
2) "2"
3) "subham"
4) "1"
This command returns the number of elements in the sorted set.
zadd user:1 1 "subham"
zadd user:1 2 "codexam"
zadd user:1 3 "xamcodexam"
zcard user:1
(integer) 3
This command returns the number of elements in the sorted set with a score between the specified minimum and maximum.
zadd user:1 1 "subham"
zadd user:1 2 "codexam"
zadd user:1 3 "xamcodexam"
zcount user:1 1 2
(integer) 2
This command increments the score of the specified member in the sorted set by the specified value.
zadd user:1 1 "subham"
zincrby user:1 2 "subham"
zrange user:1 0 -1 withscores
1) "subham"
2) "3"
This command returns the number of elements in the sorted set between the specified minimum and maximum.
zadd user:1 1 "subham"
zadd user:1 2 "codexam"
zadd user:1 3 "xamcodexam"
zlexcount user:1 - +
(integer) 3
This command returns the specified range of elements from the sorted set by lexicographical order.
zadd user:1 1 "subham"
zadd user:1 2 "codexam"
zadd user:1 3 "xamcodexam"
zrangebylex user:1 - +
1) "codexam"
2) "subham"
3) "xamcodexam"
This command returns the specified range of elements from the sorted set by lexicographical order in reverse order.
zadd user:1 1 "subham"
zadd user:1 2 "codexam"
zadd user:1 3 "xamcodexam"
zrevrangebylex user:1 + -
1) "xamcodexam"
2) "subham"
3) "codexam"
This command removes the specified range of elements from the sorted set by lexicographical order.
zadd user:1 1 "subham"
zadd user:1 2 "codexam"
zadd user:1 3 "xamcodexam"
zremrangebylex user:1 - +
(integer) 3
This command removes the specified range of elements from the sorted set by index.
zadd user:1 1 "subham"
zadd user:1 2 "codexam"
zadd user:1 3 "xamcodexam"
zremrangebyrank user:1 0 1
(integer) 2
This command removes the specified range of elements from the sorted set by score.
zadd user:1 1 "subham"
zadd user:1 2 "codexam"
zadd user:1 3 "xamcodexam"
zremrangebyscore user:1 1 2
(integer) 2
This command removes the specified range of elements from the sorted set by score.
zadd score 1 "subham"
zadd score 2 "codexam"
zadd score 3 "xamcodexam"
zrange score 0 -2 withscores
1) "subham"
2) "1"
3) "codexam"
4) "2"
This command stores the union of all the sorted sets specified.
zadd user:1 1 "subham"
zadd user:1 2 "codexam"
zadd user:2 3 "xamcodexam"
zunionstore user:3 2 user:1 user:2
(integer) 3
zrange user:3 0 -1 withscores
1) "subham"
2) "1"
3) "codexam"
4) "2"
5) "xamcodexam"
6) "3"
This command stores the intersection of all the sorted sets specified.
zadd user:1 1 "subham"
zadd user:1 2 "codexam"
zadd user:2 3 "xamcodexam"
zinterstore user:3 2 user:1 user:2
(integer) 1
zrange user:3 0 -1 withscores
1) "xamcodexam"
2) "3"
This command scans the sorted set for members matching the specified pattern.
zadd user:1 1 "subham"
zadd user:1 2 "codexam"
zadd user:2 3 "xamcodexam"
zscan user:1 0 match "sub*"
1) "0"
2) 1) "subham"
2) "1"
This command returns the rank of the specified member in the sorted set.
zadd user:1 1 "subham"
zadd user:1 2 "codexam"
zrank user:1 "subham"
(integer) 0
Note: Normally, sets are used to store unique elements. However, sorted sets allow you to store duplicate elements. This is because each element in a sorted set is associated with a score, which makes it unique and normal sets are unordered.