English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Redis Move Command

Redis Key (key)

The Redis MOVE command is used to move the key from the current database to the specified database db.

Syntax

Basic syntax of the redis Move command is as follows:

redis 127.0.0.1:6379> MOVE KEY_NAME DESTINATION_DATABASE

Available version

>= 1.0.0

Return value

Return value when moved successfully 1 , return 0 if failed.

Online Examples

# The key exists in the current database
redis> SELECT 0                             # Redis uses database 0 by default, explicitly specify it again for clarity.
OK
redis> SET song "secret base - Zone"
OK
redis> MOVE song 1                          # Move song to database 1
(integer) 1
redis> EXISTS song                          # song has been moved
(integer) 0
redis> SELECT 1                             # Use database 1
OK
redis:1> EXISTS song                        # Confirm that song has been moved to the database 1 (Note that the command prompt has changed to "redis:1" indicates that the database is being used 1)
(integer) 1
# When the key does not exist
redis:1> EXISTS fake_key
(integer) 0
redis:1> MOVE fake_key 0                    # Attempt to move from database 1 Move a non-existent key to database 0, failed
(integer) 0
redis:1> select 0                           # Use database 0
OK
redis> EXISTS fake_key                      # Verify that fake_key does not exist
(integer) 0
# When the source database and the target database have the same key
redis> SELECT 0                             # Use database 0
OK
redis> SET favorite_fruit "banana"
OK
redis> SELECT 1                             # Use database1
OK
redis:1> SET favorite_fruit "apple"
OK
redis:1> SELECT 0                           # Use database 0 and try to move favorite_fruit to the database 1
OK
redis> MOVE favorite_fruit 1                # Because two databases have the same key, the MOVE command fails
(integer) 0
redis> GET favorite_fruit                   # The favorite_fruit in database 0 has not changed
"banana"
redis> SELECT 1
OK
redis:1> GET favorite_fruit                 # Database 1 is also the favorite_fruit of
"apple"

Redis Key (key)