ZMOVEMEMBER

ZMOVEMEMBER source destination [NX | XX] MEMBERS num-members member
  [member ...]
Available since:
Redis Open Source 8.12.0
Time complexity:
O(K*(log(N)+log(M))) where K is the number of given members and N and M are the source and destination sizes.
ACL categories:
@write, @sortedset, @slow,
Compatibility:
Redis Software and Redis Cloud compatibility

Atomically moves named members from the sorted set at source to the sorted set at destination, preserving their scores.

By default, a moved member's source score replaces its score if it already exists in destination. Use NX to move only members absent from the destination, or XX to move only members already present there. A member that does not satisfy the condition stays in the source.

Other destination members are retained. Redis creates destination only when needed and deletes source when its last member is moved. Existing key expiration times are preserved; a newly created destination has no expiration.

Missing source members are skipped. Duplicates are moved only once, and moved members are returned in request order. If no requested member can move, the command returns null. When both keys name the same sorted set, it returns null without looking up the requested members.

If source exists and either key holds another type, the command returns a WRONGTYPE error before moving any member.

In Redis Open Source Cluster, both keys must be in the same hash slot. See multi-key operations.

Use ZMOVE to select by lowest score, highest score, or score range. Use BZMOVE to wait for members to become available.

Required arguments

source

The key of the sorted set to move members from.

destination

The key of the sorted set to move members to.

MEMBERS num-members member [member ...]

MEMBERS starts the list of names to look up in source. num-members must be a positive integer, followed by exactly that many member names.

The explicit block separates member names from optional arguments. Names such as NX, XX, MEMBERS, and numeric strings remain valid members. This also leaves room for additional options outside the counted block.

Optional arguments

NX | XX
  • NX moves only members that do not exist in destination.
  • XX moves only members that already exist in destination.

The options are mutually exclusive. Specify the condition before or after the complete MEMBERS block. Without either option, every requested member found in source can move, whether or not it exists in destination.

Examples

Move a named member, skipping missing names and duplicates:

Move named members: Use ZMOVEMEMBER to move specific members between sorted sets, skipping missing and duplicate names
> ZADD source:{example} 1 one 2 two
(integer) 2
> ZADD destination:{example} 99 two 100 retained
(integer) 2
> ZMOVEMEMBER source:{example} destination:{example} MEMBERS 3 missing two two
1) 1) "two"
   2) "2"
> ZRANGE source:{example} 0 -1 WITHSCORES
1) "one"
2) "1"
> ZRANGE destination:{example} 0 -1 WITHSCORES
1) "two"
2) "2"
3) "retained"
4) "100"
> ZMOVEMEMBER source:{example} destination:{example} MEMBERS 1 missing
(nil)
res13 = r.zadd("source:{example}", {"one": 1, "two": 2})
print(res13)
# >>> 2

res14 = r.zadd("destination:{example}", {"two": 99, "retained": 100})
print(res14)
# >>> 2

res15 = r.zmovemember("source:{example}", "destination:{example}", ["missing", "two", "two"])
print(res15)
# >>> [['two', 2.0]]

res16 = r.zrange("source:{example}", 0, -1, withscores=True)
print(res16)
# >>> [('one', 1.0)]

res17 = r.zrange("destination:{example}", 0, -1, withscores=True)
print(res17)
# >>> [('two', 2.0), ('retained', 100.0)]
const res13 = await client.zAdd('source:{example}', [
  { score: 1, value: 'one' },
  { score: 2, value: 'two' }
]);
console.log(res13);  // >>> 2

const res14 = await client.zAdd('destination:{example}', [
  { score: 99, value: 'two' },
  { score: 100, value: 'retained' }
]);
console.log(res14);  // >>> 2

const res15 = await client.zMoveMember('source:{example}', 'destination:{example}', ['missing', 'two', 'two']);
console.log(res15);  // >>> [ { value: 'two', score: 2 } ]

const res16 = await client.zRangeWithScores('source:{example}', 0, -1);
console.log(res16);  // >>> [ { value: 'one', score: 1 } ]

const res17 = await client.zRangeWithScores('destination:{example}', 0, -1);
console.log(res17);  // >>> [ { value: 'two', score: 2 }, { value: 'retained', score: 100 } ]
    long res13 = jedis.zadd("source:{example}", new HashMap<String, Double>() {{
      put("one", 1d);
      put("two", 2d);
    }});
    System.out.println(res13); // >>> 2

    long res14 = jedis.zadd("destination:{example}", new HashMap<String, Double>() {{
      put("two", 99d);
      put("retained", 100d);
    }});
    System.out.println(res14); // >>> 2

    List<Tuple> res15 = jedis.zmovemember("source:{example}", "destination:{example}", "missing", "two", "two");
    System.out.println(res15); // >>> [[two,2.0]]

    List<Tuple> res16 = jedis.zrangeWithScores("source:{example}", 0, -1);
    System.out.println(res16); // >>> [[one,1.0]]

    List<Tuple> res17 = jedis.zrangeWithScores("destination:{example}", 0, -1);
    System.out.println(res17); // >>> [[two,2.0], [retained,100.0]]
	res13, err := rdb.ZAdd(ctx, "source:{example}",
		redis.Z{Member: "one", Score: 1},
		redis.Z{Member: "two", Score: 2},
	).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(res13) // >>> 2

	res14, err := rdb.ZAdd(ctx, "destination:{example}",
		redis.Z{Member: "two", Score: 99},
		redis.Z{Member: "retained", Score: 100},
	).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(res14) // >>> 2

	res15, err := rdb.ZMoveMember(ctx, "source:{example}", "destination:{example}",
		"missing", "two", "two",
	).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(res15) // >>> [{2 two}]

	res16, err := rdb.ZRangeWithScores(ctx, "source:{example}", 0, -1).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(res16) // >>> [{1 one}]

	res17, err := rdb.ZRangeWithScores(ctx, "destination:{example}", 0, -1).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(res17) // >>> [{2 two} {100 retained}]
        $res13 = $r->zadd('source:{example}', ['one' => 1, 'two' => 2]);
        echo $res13 . PHP_EOL;
        // >>> 2

        $res14 = $r->zadd('destination:{example}', ['two' => 99, 'retained' => 100]);
        echo $res14 . PHP_EOL;
        // >>> 2

        $res15 = $r->zmovemember('source:{example}', 'destination:{example}', ['missing', 'two', 'two']);
        echo json_encode($res15) . PHP_EOL;
        // >>> {"two":2}

        $res16 = $r->zrange('source:{example}', 0, -1, ['withscores' => true]);
        echo json_encode($res16) . PHP_EOL;
        // >>> {"one":"1"}

        $res17 = $r->zrange('destination:{example}', 0, -1, ['withscores' => true]);
        echo json_encode($res17) . PHP_EOL;
        // >>> {"two":"2","retained":"100"}

Condition moves on destination membership:

Conditional move: Use ZMOVEMEMBER with NX or XX to move members only when they are missing from, or already in, the destination
> ZADD source:{conditional} 1 existing 2 new
(integer) 2
> ZADD destination:{conditional} 99 existing
(integer) 1
> ZMOVEMEMBER source:{conditional} destination:{conditional} NX MEMBERS 2 existing new
1) 1) "new"
   2) "2"
> ZSCORE source:{conditional} existing
"1"
> ZMOVEMEMBER source:{conditional} destination:{conditional} MEMBERS 1 existing XX
1) 1) "existing"
   2) "1"
> ZRANGE destination:{conditional} 0 -1 WITHSCORES
1) "existing"
2) "1"
3) "new"
4) "2"
res18 = r.zadd("source:{conditional}", {"existing": 1, "new": 2})
print(res18)
# >>> 2

res19 = r.zadd("destination:{conditional}", {"existing": 99})
print(res19)
# >>> 1

res20 = r.zmovemember(
    "source:{conditional}", "destination:{conditional}", ["existing", "new"], nx=True
)
print(res20)
# >>> [['new', 2.0]]

res21 = r.zscore("source:{conditional}", "existing")
print(res21)
# >>> 1.0

res22 = r.zmovemember(
    "source:{conditional}", "destination:{conditional}", ["existing"], xx=True
)
print(res22)
# >>> [['existing', 1.0]]

res23 = r.zrange("destination:{conditional}", 0, -1, withscores=True)
print(res23)
# >>> [('existing', 1.0), ('new', 2.0)]
const res18 = await client.zAdd('source:{conditional}', [
  { score: 1, value: 'existing' },
  { score: 2, value: 'new' }
]);
console.log(res18);  // >>> 2

const res19 = await client.zAdd('destination:{conditional}', { score: 99, value: 'existing' });
console.log(res19);  // >>> 1

const res20 = await client.zMoveMember(
  'source:{conditional}', 'destination:{conditional}', ['existing', 'new'], { condition: 'NX' }
);
console.log(res20);  // >>> [ { value: 'new', score: 2 } ]

const res21 = await client.zScore('source:{conditional}', 'existing');
console.log(res21);  // >>> 1

const res22 = await client.zMoveMember(
  'source:{conditional}', 'destination:{conditional}', 'existing', { condition: 'XX' }
);
console.log(res22);  // >>> [ { value: 'existing', score: 1 } ]

const res23 = await client.zRangeWithScores('destination:{conditional}', 0, -1);
console.log(res23);  // >>> [ { value: 'existing', score: 1 }, { value: 'new', score: 2 } ]
    long res18 = jedis.zadd("source:{conditional}", new HashMap<String, Double>() {{
      put("existing", 1d);
      put("new", 2d);
    }});
    System.out.println(res18); // >>> 2

    long res19 = jedis.zadd("destination:{conditional}", new HashMap<String, Double>() {{
      put("existing", 99d);
    }});
    System.out.println(res19); // >>> 1

    List<Tuple> res20 = jedis.zmovemember("source:{conditional}", "destination:{conditional}",
        ZMoveMemberParams.zMoveMemberParams().nx(), "existing", "new");
    System.out.println(res20); // >>> [[new,2.0]]

    Double res21 = jedis.zscore("source:{conditional}", "existing");
    System.out.println(res21); // >>> 1.0

    Tuple res22 = jedis.zmovemember("source:{conditional}", "destination:{conditional}",
        ZMoveMemberParams.zMoveMemberParams().xx(), "existing");
    System.out.println(res22); // >>> [existing,1.0]

    List<Tuple> res23 = jedis.zrangeWithScores("destination:{conditional}", 0, -1);
    System.out.println(res23); // >>> [[existing,1.0], [new,2.0]]
	res18, err := rdb.ZAdd(ctx, "source:{conditional}",
		redis.Z{Member: "existing", Score: 1},
		redis.Z{Member: "new", Score: 2},
	).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(res18) // >>> 2

	res19, err := rdb.ZAdd(ctx, "destination:{conditional}",
		redis.Z{Member: "existing", Score: 99},
	).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(res19) // >>> 1

	res20, err := rdb.ZMoveMemberWithArgs(ctx,
		"source:{conditional}", "destination:{conditional}",
		redis.ZMoveMemberNX, "existing", "new",
	).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(res20) // >>> [{2 new}]

	res21, err := rdb.ZScore(ctx, "source:{conditional}", "existing").Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(res21) // >>> 1

	res22, err := rdb.ZMoveMemberWithArgs(ctx,
		"source:{conditional}", "destination:{conditional}",
		redis.ZMoveMemberXX, "existing",
	).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(res22) // >>> [{1 existing}]

	res23, err := rdb.ZRangeWithScores(ctx, "destination:{conditional}", 0, -1).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(res23) // >>> [{1 existing} {2 new}]
        $res18 = $r->zadd('source:{conditional}', ['existing' => 1, 'new' => 2]);
        echo $res18 . PHP_EOL;
        // >>> 2

        $res19 = $r->zadd('destination:{conditional}', ['existing' => 99]);
        echo $res19 . PHP_EOL;
        // >>> 1

        $res20 = $r->zmovemember('source:{conditional}', 'destination:{conditional}', ['existing', 'new'], 'NX');
        echo json_encode($res20) . PHP_EOL;
        // >>> {"new":2}

        $res21 = $r->zscore('source:{conditional}', 'existing');
        echo $res21 . PHP_EOL;
        // >>> 1

        $res22 = $r->zmovemember('source:{conditional}', 'destination:{conditional}', ['existing'], 'XX');
        echo json_encode($res22) . PHP_EOL;
        // >>> {"existing":1}

        $res23 = $r->zrange('destination:{conditional}', 0, -1, ['withscores' => true]);
        echo json_encode($res23) . PHP_EOL;
        // >>> {"existing":"1","new":"2"}

Use option names as literal members by including them in the counted list:

redis> ZADD source:{tokens} 1 NX 2 XX 3 MEMBERS 4 1
(integer) 4
redis> ZMOVEMEMBER source:{tokens} destination:{tokens} NX MEMBERS 4 NX XX MEMBERS 1
1) 1) "NX"
   2) "1"
2) 1) "XX"
   2) "2"
3) 1) "MEMBERS"
   2) "3"
4) 1) "1"
   2) "4"

The NX before MEMBERS is the condition. The four names in the counted block are literal members, including the second MEMBERS.

Redis Software and Redis Cloud compatibility

Redis
Software
Redis
Cloud
Notes
❌ Standard
❌ Active-Active
❌ Standard
❌ Active-Active

Return information

One of the following:

  • Array reply: an array of two-element member/score arrays in request order, nested even when only one member is moved. Each score is a bulk string.
  • Nil reply: source does not exist, none of the requested members can move, or both keys name the same sorted set.
RATE THIS PAGE
Back to top ↑