In this tutorial, we will learn about how to perform the intersection operation on two or more sets value stored in redis datastore, by using redis SINTER and SINTERSTORE command.
Intersection of Sets:
In set theory, the intersection of two or more sets is the set which contains the elements that are common to all the sets. For example :
A = {1, 2, 3, 4, 5} B = {4, 5, 6, 7, 8, 9} Intersection of A & B :- A ∩ B = {4, 5}
SINTER Command :-
This command perform the intersection operation on two or more specified sets and returns the result as an array. If any of the specified key doesn’t exist, then it will considered as an empty set. Error will be returned, if key exist but value stored at the key is not a set. The syntax of redis SINTER command is as follows :-
Syntax :-
redis host:post> SINTER <keyName 1> <keyName 2> <keyName 3>
Output :-
- (array) reply, containing elements resulting from the intersection operation. - Error, if key exist and value stored at the key is not a set.
Example :-
SINTERSTORE Command :-
This command perform the intersection operation on two or more specified sets and returns a new set value stored at the specified key. If any of the specified key doesn’t exist, then it will considered as an empty set. Error will be returned, if key exist but value stored at the key is not a set. The syntax of redis SINTERSTORE command is as follows :-
Syntax :-
redis host:post> SINTERSTORE <destination keyName> <keyName 1> <keyName 2> <keyName 3>
Output :-
- (integer) representing number of elements in the destination set. - Error, if key exist and value stored at the key is not a set.
Example :-
References :-
- SINTER Command Docs
- SINTERSTORE Command Docs
That’s all for how to perform the intersection operation on two or more set values stored in redis datastore. If you liked it, please share your thoughts in comments section and share it with others too.