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

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:

  • ZPOPMIN or ZPOPMAX which take only one key, and can return multiple elements.
  • BZPOPMIN or BZPOPMAX which take multiple keys, but return only one element from just one key.

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.

*Return value

Array reply: specifically:

  • A nil when no element could be popped.
  • 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.

*Examples

redis>  ZMPOP 1 notsuchkey MIN
ERR Don't know what to do for "zmpop"
redis>  ZADD myzset 1 "one" 2 "two" 3 "three"
(integer) 3
redis>  ZMPOP 1 myzset MIN
ERR Don't know what to do for "zmpop"
redis>  ZRANGE myzset 0 -1 WITHSCORES
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
redis>  ZMPOP 1 myzset MAX COUNT 10
ERR Don't know what to do for "zmpop"
redis>  ZADD myzset2 4 "four" 5 "five" 6 "six"
(integer) 3
redis>  ZMPOP 2 myzset myzset2 MIN COUNT 10
ERR Don't know what to do for "zmpop"
redis>  ZRANGE myzset 0 -1 WITHSCORES
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
redis>  ZMPOP 2 myzset myzset2 MAX COUNT 10
ERR Don't know what to do for "zmpop"
redis>  ZRANGE myzset2 0 -1 WITHSCORES
1) "four"
2) "4"
3) "five"
4) "5"
5) "six"
6) "6"
redis>  EXISTS myzset myzset2
(integer) 2
redis>