Redis - Set Smove Command



Redis SMOVE command is used to move an element of a set from one key to another key. If the source set does not exist or does not contain the specified element, no operation is performed and 0 is returned. Otherwise, the element is removed from the source set and added to the destination set. When the specified element already exists in the destination set, it is only removed from the source set. An error is returned, if the source or destination does not hold a set value.

Return Value

Integer reply.

  • 1, if the element is moved.

  • 0, if the element is not a member of the source and no operation was performed.

Syntax

Following is the basic syntax of Redis SMOVE command.

redis 127.0.0.1:6379> SMOVE SOURCE DESTINATION MEMBER

Example

redis 127.0.0.1:6379> SADD myset1 "hello" 
(integer) 1 
redis 127.0.0.1:6379> SADD myset1 "world" 
(integer) 1 
redis 127.0.0.1:6379> SADD myset1 "bar" 
(integer) 1 
redis 127.0.0.1:6379> SADD myset2 "foo" 
(integer) 1
redis 127.0.0.1:6379> SMOVE myset1 myset2 "bar" 
(integer) 1 
redis 127.0.0.1:6379> SMEMBERS myset1 
1) "World" 
2) "Hello" 
redis 127.0.0.1:6379> SMEMBERS myset2 
1) "foo" 
2) "bar" 
redis_sets.htm
Advertisements