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
NXmoves only members that do not exist indestination.XXmoves only members that already exist indestination.
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:
> 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)]
import redis
r = redis.Redis(decode_responses=True)
res1 = r.zadd("scores:{example}", {"one": 1, "two": 2, "three": 3})
print(res1)
# >>> 3
res2 = r.zadd("moved:{example}", {"one": 99, "retained": 100})
print(res2)
# >>> 2
res3 = r.zmove("scores:{example}", "moved:{example}", min=True)
print(res3)
# >>> [['one', 1.0]]
res4 = r.zmove("scores:{example}", "moved:{example}", max=True)
print(res4)
# >>> [['three', 3.0]]
res5 = r.zrange("moved:{example}", 0, -1, withscores=True)
print(res5)
# >>> [('one', 1.0), ('three', 3.0), ('retained', 100.0)]
res6 = r.zrange("scores:{example}", 0, -1, withscores=True)
print(res6)
# >>> [('two', 2.0)]
res7 = r.zadd("ready:{example}", {"first": 1, "second": 2})
print(res7)
# >>> 2
res8 = r.bzmove("ready:{example}", "processing:{example}", 1, min=True, count=3)
print(res8)
# >>> [['first', 1.0], ['second', 2.0]]
res9 = r.zadd(
"bookmarks:{user:42}",
{"bookmark:a": 1699999998, "bookmark:b": 1699999999, "bookmark:c": 1700000000},
)
print(res9)
# >>> 3
res10 = r.zmove(
"bookmarks:{user:42}",
"batch:{user:42}:001",
byscore=("-inf", "(1700000000"),
count=500,
)
print(res10)
# >>> [['bookmark:a', 1699999998.0], ['bookmark:b', 1699999999.0]]
res11 = r.zrange("bookmarks:{user:42}", 0, -1, withscores=True)
print(res11)
# >>> [('bookmark:c', 1700000000.0)]
res12 = r.zmove(
"bookmarks:{user:42}",
"batch:{user:42}:002",
byscore=("-inf", "(1700000000"),
count=500,
)
print(res12)
# >>> None
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)]
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 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 } ]
import assert from 'node:assert';
import { createClient } from 'redis';
const client = createClient();
await client.connect();
const res1 = await client.zAdd('scores:{example}', [
{ score: 1, value: 'one' },
{ score: 2, value: 'two' },
{ score: 3, value: 'three' }
]);
console.log(res1); // >>> 3
const res2 = await client.zAdd('moved:{example}', [
{ score: 99, value: 'one' },
{ score: 100, value: 'retained' }
]);
console.log(res2); // >>> 2
const res3 = await client.zMove('scores:{example}', 'moved:{example}', 'MIN');
console.log(res3); // >>> [ { value: 'one', score: 1 } ]
const res4 = await client.zMove('scores:{example}', 'moved:{example}', 'MAX');
console.log(res4); // >>> [ { value: 'three', score: 3 } ]
const res5 = await client.zRangeWithScores('moved:{example}', 0, -1);
console.log(res5);
// >>> [ { value: 'one', score: 1 }, { value: 'three', score: 3 }, { value: 'retained', score: 100 } ]
const res6 = await client.zRangeWithScores('scores:{example}', 0, -1);
console.log(res6); // >>> [ { value: 'two', score: 2 } ]
const res7 = await client.zAdd('ready:{example}', [
{ score: 1, value: 'first' },
{ score: 2, value: 'second' }
]);
console.log(res7); // >>> 2
const res8 = await client.bzMove('ready:{example}', 'processing:{example}', 'MIN', 1, { COUNT: 3 });
console.log(res8); // >>> [ { value: 'first', score: 1 }, { value: 'second', score: 2 } ]
const res9 = await client.zAdd('bookmarks:{user:42}', [
{ score: 1699999998, value: 'bookmark:a' },
{ score: 1699999999, value: 'bookmark:b' },
{ score: 1700000000, value: 'bookmark:c' }
]);
console.log(res9); // >>> 3
const res10 = await client.zMove(
'bookmarks:{user:42}',
'batch:{user:42}:001',
{ min: '-inf', max: '(1700000000' },
{ COUNT: 500 }
);
console.log(res10);
// >>> [ { value: 'bookmark:a', score: 1699999998 }, { value: 'bookmark:b', score: 1699999999 } ]
const res11 = await client.zRangeWithScores('bookmarks:{user:42}', 0, -1);
console.log(res11); // >>> [ { value: 'bookmark:c', score: 1700000000 } ]
const res12 = await client.zMove(
'bookmarks:{user:42}',
'batch:{user:42}:002',
{ min: '-inf', max: '(1700000000' },
{ COUNT: 500 }
);
console.log(res12); // >>> null
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 } ]
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 } ]
await client.close();
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]]
package io.redis.examples;
import redis.clients.jedis.RedisClient;
import redis.clients.jedis.args.SortedSetOption;
import redis.clients.jedis.params.ZMoveMemberParams;
import redis.clients.jedis.resps.Tuple;
public class CmdsZmoveExample {
public void run() {
RedisClient jedis = RedisClient.create("redis://localhost:6379");
long res1 = jedis.zadd("scores:{example}", new HashMap<String, Double>() {{
put("one", 1d);
put("two", 2d);
put("three", 3d);
}});
System.out.println(res1); // >>> 3
long res2 = jedis.zadd("moved:{example}", new HashMap<String, Double>() {{
put("one", 99d);
put("retained", 100d);
}});
System.out.println(res2); // >>> 2
List<Tuple> res3 = jedis.zmove("scores:{example}", "moved:{example}", SortedSetOption.MIN);
System.out.println(res3); // >>> [[one,1.0]]
List<Tuple> res4 = jedis.zmove("scores:{example}", "moved:{example}", SortedSetOption.MAX);
System.out.println(res4); // >>> [[three,3.0]]
List<Tuple> res5 = jedis.zrangeWithScores("moved:{example}", 0, -1);
System.out.println(res5); // >>> [[one,1.0], [three,3.0], [retained,100.0]]
List<Tuple> res6 = jedis.zrangeWithScores("scores:{example}", 0, -1);
System.out.println(res6); // >>> [[two,2.0]]
long res7 = jedis.zadd("ready:{example}", new HashMap<String, Double>() {{
put("first", 1d);
put("second", 2d);
}});
System.out.println(res7); // >>> 2
List<Tuple> res8 = jedis.bzmove("ready:{example}", "processing:{example}", SortedSetOption.MIN, 1, 3);
System.out.println(res8); // >>> [[first,1.0], [second,2.0]]
long res9 = jedis.zadd("bookmarks:{user:42}", new HashMap<String, Double>() {{
put("bookmark:a", 1699999998d);
put("bookmark:b", 1699999999d);
put("bookmark:c", 1700000000d);
}});
System.out.println(res9); // >>> 3
List<Tuple> res10 = jedis.zmoveByScore("bookmarks:{user:42}", "batch:{user:42}:001", "-inf", "(1700000000", 500);
System.out.println(res10); // >>> [[bookmark:a,1.699999998E9], [bookmark:b,1.699999999E9]]
List<Tuple> res11 = jedis.zrangeWithScores("bookmarks:{user:42}", 0, -1);
System.out.println(res11); // >>> [[bookmark:c,1.7E9]]
List<Tuple> res12 = jedis.zmoveByScore("bookmarks:{user:42}", "batch:{user:42}:002", "-inf", "(1700000000", 500);
System.out.println(res12); // >>> null
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]]
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]]
jedis.close();
}
}
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}]
package example_commands_test
import (
"context"
"fmt"
"time"
"github.com/redis/go-redis/v9"
)
func ExampleClient_zmove() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res1, err := rdb.ZAdd(ctx, "scores:{example}",
redis.Z{Member: "one", Score: 1},
redis.Z{Member: "two", Score: 2},
redis.Z{Member: "three", Score: 3},
).Result()
if err != nil {
panic(err)
}
fmt.Println(res1) // >>> 3
res2, err := rdb.ZAdd(ctx, "moved:{example}",
redis.Z{Member: "one", Score: 99},
redis.Z{Member: "retained", Score: 100},
).Result()
if err != nil {
panic(err)
}
fmt.Println(res2) // >>> 2
res3, err := rdb.ZMove(ctx, "scores:{example}", "moved:{example}", "MIN").Result()
if err != nil {
panic(err)
}
fmt.Println(res3) // >>> [{1 one}]
res4, err := rdb.ZMove(ctx, "scores:{example}", "moved:{example}", "MAX").Result()
if err != nil {
panic(err)
}
fmt.Println(res4) // >>> [{3 three}]
res5, err := rdb.ZRangeWithScores(ctx, "moved:{example}", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res5) // >>> [{1 one} {3 three} {100 retained}]
res6, err := rdb.ZRangeWithScores(ctx, "scores:{example}", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res6) // >>> [{2 two}]
}
func ExampleClient_bzmove() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res7, err := rdb.ZAdd(ctx, "ready:{example}",
redis.Z{Member: "first", Score: 1},
redis.Z{Member: "second", Score: 2},
).Result()
if err != nil {
panic(err)
}
fmt.Println(res7) // >>> 2
res8, err := rdb.BZMoveCount(ctx,
"ready:{example}", "processing:{example}", "MIN", time.Second, 3,
).Result()
if err != nil {
panic(err)
}
fmt.Println(res8) // >>> [{1 first} {2 second}]
}
func ExampleClient_zmove_byscore() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res9, err := rdb.ZAdd(ctx, "bookmarks:{user:42}",
redis.Z{Member: "bookmark:a", Score: 1699999998},
redis.Z{Member: "bookmark:b", Score: 1699999999},
redis.Z{Member: "bookmark:c", Score: 1700000000},
).Result()
if err != nil {
panic(err)
}
fmt.Println(res9) // >>> 3
res10, err := rdb.ZMoveByScoreCount(ctx,
"bookmarks:{user:42}", "batch:{user:42}:001",
"-inf", "(1700000000", 500,
).Result()
if err != nil {
panic(err)
}
fmt.Println(res10) // >>> [{1.699999998e+09 bookmark:a} {1.699999999e+09 bookmark:b}]
res11, err := rdb.ZRangeWithScores(ctx, "bookmarks:{user:42}", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res11) // >>> [{1.7e+09 bookmark:c}]
_, err = rdb.ZMoveByScoreCount(ctx,
"bookmarks:{user:42}", "batch:{user:42}:002",
"-inf", "(1700000000", 500,
).Result()
fmt.Println(err == redis.Nil) // >>> true
}
func ExampleClient_zmovemember() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
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}]
}
func ExampleClient_zmovemember_cond() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
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}]
}
$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"}
<?php
use Predis\Client as PredisClient;
class CmdsZmoveTest
{
public function testCmdsZmove() {
$r = new PredisClient([
'scheme' => 'tcp',
'host' => '127.0.0.1',
'port' => 6379,
'password' => '',
'database' => 0,
]);
$res1 = $r->zadd('scores:{example}', ['one' => 1, 'two' => 2, 'three' => 3]);
echo $res1 . PHP_EOL;
// >>> 3
$res2 = $r->zadd('moved:{example}', ['one' => 99, 'retained' => 100]);
echo $res2 . PHP_EOL;
// >>> 2
$res3 = $r->zmove('scores:{example}', 'moved:{example}', 'MIN');
echo json_encode($res3) . PHP_EOL;
// >>> {"one":1}
$res4 = $r->zmove('scores:{example}', 'moved:{example}', 'MAX');
echo json_encode($res4) . PHP_EOL;
// >>> {"three":3}
$res5 = $r->zrange('moved:{example}', 0, -1, ['withscores' => true]);
echo json_encode($res5) . PHP_EOL;
// >>> {"one":"1","three":"3","retained":"100"}
$res6 = $r->zrange('scores:{example}', 0, -1, ['withscores' => true]);
echo json_encode($res6) . PHP_EOL;
// >>> {"two":"2"}
$res7 = $r->zadd('ready:{example}', ['first' => 1, 'second' => 2]);
echo $res7 . PHP_EOL;
// >>> 2
$res8 = $r->bzmove('ready:{example}', 'processing:{example}', 'MIN', 1, 3);
echo json_encode($res8) . PHP_EOL;
// >>> {"first":1,"second":2}
$res9 = $r->zadd('bookmarks:{user:42}', [
'bookmark:a' => 1699999998,
'bookmark:b' => 1699999999,
'bookmark:c' => 1700000000,
]);
echo $res9 . PHP_EOL;
// >>> 3
$res10 = $r->zmove('bookmarks:{user:42}', 'batch:{user:42}:001', ['-inf', '(1700000000'], 500);
echo json_encode($res10) . PHP_EOL;
// >>> {"bookmark:a":1699999998,"bookmark:b":1699999999}
$res11 = $r->zrange('bookmarks:{user:42}', 0, -1, ['withscores' => true]);
echo json_encode($res11) . PHP_EOL;
// >>> {"bookmark:c":"1700000000"}
$res12 = $r->zmove('bookmarks:{user:42}', 'batch:{user:42}:002', ['-inf', '(1700000000'], 500);
echo json_encode($res12) . PHP_EOL;
// >>> null
$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"}
$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"}
}
}
Condition moves on destination membership:
> 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 |
|---|---|---|
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:
sourcedoes not exist, none of the requested members can move, or both keys name the same sorted set.