zmpop.3valkey - Man Page

Returns the highest- or lowest-scoring members from one or more sorted sets after removing them. Deletes the sorted set if the last member was popped.

Synopsis

ZMPOP numkeys key [key...] <MIN | MAX> [COUNT count]

Description

Pops one or more elements, that are member-score pairs, from the first non-empty sorted set in the provided list of key names.

ZMPOP and BZMPOP are similar to the following, more limited, commands:

See BZMPOP for the blocking variant of this command.

When the MIN modifier is used, the elements popped are those with the lowest scores from the first non-empty sorted set. The MAX modifier causes elements with the highest scores to be popped. The optional COUNT can be used to specify the number of elements to pop, and is set to 1 by default.

The number of popped elements is the minimum from the sorted set’s cardinality and COUNT’s value.

Reply

Resp2

One of the following:

  • valkey-protocol(7) Nil reply: when no element could be popped.
  • valkey-protocol(7) Array reply: A two-element array with the first element being the name of the key from which elements were popped, and the second element is an array of the popped elements. Every entry in the elements array is also an array that contains the member and its score.

Resp3

One of the following:

  • valkey-protocol(7) Null reply: when no element could be popped.
  • valkey-protocol(7) Array reply: A two-element array with the first element being the name of the key from which elements were popped, and the second element is an array of the popped elements. Every entry in the elements array is also an array that contains the member and its score.

Complexity

O(K) + O(M*log(N)) where K is the number of provided keys, N being the number of elements in the sorted set, and M being the number of elements popped.

Acl Categories

@slow @sortedset @write

History

Examples

127.0.0.1:6379> ZMPOP 1 notsuchkey MIN
(nil)
127.0.0.1:6379> ZADD myzset 1 "one" 2 "two" 3 "three"
(integer) 3
127.0.0.1:6379> ZMPOP 1 myzset MIN
1) "myzset"
2) 1) 1) "one"
      2) "1"
127.0.0.1:6379> ZRANGE myzset 0 -1 WITHSCORES
1) "two"
2) "2"
3) "three"
4) "3"
127.0.0.1:6379> ZMPOP 1 myzset MAX COUNT 10
1) "myzset"
2) 1) 1) "three"
      2) "3"
   2) 1) "two"
      2) "2"
127.0.0.1:6379> ZADD myzset2 4 "four" 5 "five" 6 "six"
(integer) 3
127.0.0.1:6379> ZMPOP 2 myzset myzset2 MIN COUNT 10
1) "myzset2"
2) 1) 1) "four"
      2) "4"
   2) 1) "five"
      2) "5"
   3) 1) "six"
      2) "6"
127.0.0.1:6379> ZRANGE myzset 0 -1 WITHSCORES
(empty array)
127.0.0.1:6379> ZMPOP 2 myzset myzset2 MAX COUNT 10
(nil)
127.0.0.1:6379> ZRANGE myzset2 0 -1 WITHSCORES
(empty array)
127.0.0.1:6379> EXISTS myzset myzset2
(integer) 0

See Also

bzmpop(3valkey), bzpopmax(3valkey), bzpopmin(3valkey), zadd(3valkey), zcard(3valkey), zcount(3valkey), zdiff(3valkey), zdiffstore(3valkey), zincrby(3valkey), zinter(3valkey), zintercard(3valkey), zinterstore(3valkey), zlexcount(3valkey), zmscore(3valkey), zpopmax(3valkey), zpopmin(3valkey), zrandmember(3valkey), zrange(3valkey), zrangestore(3valkey), zrank(3valkey), zrem(3valkey), zremrangebylex(3valkey), zremrangebyrank(3valkey), zremrangebyscore(3valkey), zrevrank(3valkey), zscan(3valkey), zscore(3valkey), zunion(3valkey), zunionstore(3valkey)

Info

2024-09-23 8.0.0 Valkey Command Manual