Redis database Operations (3)

Source: Internet
Author: User

Set type

To add an element to an unordered collection:
sadd KEY VALUE1 VALUE2 VALUE3 Example: (Sadd my_set1 a b c D D c s a)

To get the elements of an unordered collection:
smembers KEY Example: (Smembers my_set1)
Results:
1) "C"
2) "S"
3) "A"
4) "D"
5) "B"

To delete a specified element in an unordered collection:
Srem KEY VALUE Example: (Srem My_set1 a)
Query results by Smembers My_set1:
1) "C"
2) "S"
3) "D"
4) "B"

To delete a random element in an unordered collection:
Spop KEY VALUE Example: (Spop my_set1)
Query results by Smembers My_set1:
1) "C"
2) "S"
3) "B"

Move an element of a collection to another collection:
smove src_key Dst_key VALUE Example: (Smove my_set1 My_set2 s)
Query results by Smembers My_set1:
1) "C"
2) "B"
Query results by Smembers My_set2:
1) "S"

Determine whether a collection has an element:
sismember KEY VALUE Example: (Sismember My_set1 B)

Intersection:
sinter KEY1 KEY2
Cases:
Sadd My_set1 a b c d E
Sadd My_set2 C d E F g
Sinter My_set1 My_set2
Results:
1) "C"
2) "D"
3) "E"

Merge the intersection of the KEY1 KEY2 into the KEY3:
Sinterstore KEY3 KEY1 KEY2
Cases:
Sinterstore My_set3 My_set1 My_set2
Query results by Smembers My_set3:
1) "D"
2) "C"
3) "E"

and set:
sunion KEY1 KEY2
Cases:
Sunion My_set1 My_set2
Results:
1) "G"
2) "C"
3) "S"
4) "D"
5) "A"
6) "F"
7) "E"
8) "B"

Merge the Union of the KEY1 KEY2 into the KEY3:
Sunionstore KEY3 KEY1 KEY2
Cases:
Sunionstore My_set3 My_set1 My_set2
Query results by Smembers My_set3:
1) "G"
2) "C"
3) "S"
4) "D"
5) "A"
6) "F"
7) "E"
8) "B"

Difference set: (in Set 1, remove the elements in set 2, leaving the set 2 without, only in the collection 1 elements)
Sdiff KEY1 KEY2
Cases:
Sdiff My_set1 My_set2
Results:
1) "A"
2) "B"

Merge the difference set of KEY1 KEY2 to KEY3:

Sdiffstore KEY3 KEY1 KEY2
Cases:
Sdiffstore My_set3 My_set2 My_set1
Query results by Smembers My_set3:
1) "G"
2) "F"
3) "S"

Gets the number of elements in the collection:
SCard KEY
Cases:
SCard My_set1

Randomly returns an element in the collection:
Srandmember KEY
Cases:
Srandmember My_set1


Zset type

To add an element to an ordered collection:
Zadd KEY score VALUE
Cases:
Zadd my_zset 1 ' one '
Zadd My_zset 2 '
Zadd My_zset 3 ' three '
Zadd My_zset 4 ' four '
Zadd my_zset 5 ' five '

To get the elements in an ordered collection:
Zrange KEY START STOP (withscores)
Cases:
Zrange My_zset 0-1
Results:
1) "One"
2) "Both"
3) "Three"
4) "Four"
5) "Five"
Cases:
Zrange My_zset 0 3 withscores
Results:
1) "One"
2) "1"
3) "Both"
4) "2"
5) "Three"
6) "3"
7) "Four"
8) "4"

To get an element in an ordered collection in reverse order:
Zrevrange KEY START STOP (withscores)
Cases:
Zrevrange My_zset 1 3
Results:
1) "Four"
2) "Three"
3) "Both"
Cases:
Zrevrange My_zset 1-1 Withscores
1) "Four"
2) "4"
3) "Three"
4) "3"
5) "Both"
6) "2"
7) "One"
8) "1"

To delete an element in an ordered collection:
Zrem KEY VALUE
Cases:
Zrem My_zset
View results by Zrange My_zset 0-1:
1) "One"
2) "Three"
3) "Four"
4) "Five"

To find the position of an element in an ordered collection in the positive order:
Zrank KEY VALUE
Cases:
Zrank My_zset Five
Results:
3

To find the position of an element in an ordered collection in reverse order:
Zrevrank KEY VALUE
Cases:
Zrevrank My_zset Five
Results:
0

To view the number of ordered collection elements:
Zcard KEY
Cases:
Zcard My_set

Returns the elements in the collection that are score at a given interval:
Zrangebyscore KEY score_min Score_max (withscores)
Cases:
Zrangebyscore My_zset 1 4
Results:
1) "One"
2) "Three"
3) "Four"
Cases:
Zrangebyscore My_zset 1 4 withscores
Results:
1) "One"
2) "1"
3) "Three"
4) "3"
5) "Four"
6) "4"

Returns the number of elements in the collection that are score at a given interval:
Zcount KEY score_min Score_max
Cases:
Zcount My_zset 1 4

To view the score value of an element:
Zscore KEY VALUE

Cases:
Zscore My_zset Five

Deletes the element in the collection that is ranked in the given interval (forward):
Zremrangebyrank KEY START STOP
Cases:
Zremrangebyrank My_zset 0 2

Deletes the element in the collection that score at a given interval:
Zremrangebyscore KEY score_min Score_max
Cases:
Zremrangebyscore My_zset 4 5

Redis database Operations (3)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.