Back to Redis

ZMPOP

content/commands/zmpop.md

latest3.1 KB
Original Source

{{< note >}} This command's behavior varies in clustered Redis environments. See the [multi-key operations]({{< relref "/develop/using-commands/multi-key-operations" >}}) page for more information. {{< /note >}}

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]({{< relref "/commands/bzmpop" >}}) are similar to the following, more limited, commands:

  • [ZPOPMIN]({{< relref "/commands/zpopmin" >}}) or [ZPOPMAX]({{< relref "/commands/zpopmax" >}}) which take only one key, and can return multiple elements.
  • [BZPOPMIN]({{< relref "/commands/bzpopmin" >}}) or [BZPOPMAX]({{< relref "/commands/bzpopmax" >}}) which take multiple keys, but return only one element from just one key.

See [BZMPOP]({{< relref "/commands/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.

Examples

{{% redis-cli %}} ZMPOP 1 notsuchkey MIN ZADD myzset 1 "one" 2 "two" 3 "three" ZMPOP 1 myzset MIN ZRANGE myzset 0 -1 WITHSCORES ZMPOP 1 myzset MAX COUNT 10 ZADD myzset2 4 "four" 5 "five" 6 "six" ZMPOP 2 myzset myzset2 MIN COUNT 10 ZRANGE myzset 0 -1 WITHSCORES ZMPOP 2 myzset myzset2 MAX COUNT 10 ZRANGE myzset2 0 -1 WITHSCORES EXISTS myzset myzset2 {{% /redis-cli %}}

Redis Software and Redis Cloud compatibility

| Redis Software | Redis Cloud | <span style="min-width: 9em; display: table-cell">Notes</span> | |:----------------------|:-----------------|:------| | <span title="Supported">✅ Standard</span> <span title="Supported"><nobr>✅ Active-Active</nobr></span> | <span title="Supported">✅ Standard</span> <span title="Supported"><nobr>✅ Active-Active</nobr></span> | |

Return information

{{< multitabs id="zmpop-return-info" tab1="RESP2" tab2="RESP3" >}}

One of the following:

  • Nil reply: when no element could be popped.
  • 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.

-tab-sep-

One of the following:

  • Null reply: when no element could be popped.
  • 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.

{{< /multitabs >}}