ZMPOP(3valkey)

πŸ“¦
version 9.0.6 Β· 2 versions
, in fedora 44

✨ ZMPOP ✨

okay bestie so this is ZMPOP from the valkey command manual πŸ’– β€” it's the one that reaches into one or more sorted sets, yanks out the highest- or lowest-scoring members, and deletes the whole sorted set if that last member was popped. dramatic? yes. iconic? also yes. πŸ‘‘


πŸŽ€ NAME πŸŽ€

zmpop β€” returns the highest- or lowest-scoring members from one or more sorted sets after removing them. and if you popped the last member? bestie that sorted set is gone. deleted. poof. πŸ’€


🌸 SYNOPSIS 🌸

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

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so picture this: you hand it a list of key names, and it pops one or more elements β€” and each of those elements is a member-score pair β€” from the first non-empty sorted set in that list. that's the whole vibe. 🌈

ZMPOP and BZMPOP are like the glow-up versions of these more limited commands:

  • ZPOPMIN or ZPOPMAX β€” these only take one key, but they can return multiple elements.
  • BZPOPMIN or BZPOPMAX β€” these take multiple keys, but they only return one element from just one key. πŸ™ˆ

if you want the blocking version of this command, go see BZMPOP.

so when you use the MIN modifier, the elements that get popped are the ones with the lowest scores from the first non-empty sorted set. when you use MAX, it flips and the highest scores get popped instead. and the optional COUNT? that's you telling it how many elements to pop β€” it's set to 1 by default. πŸ’…

one more thing, lowkey important: the number of popped elements is the minimum of the sorted set's cardinality and COUNT's value. so it never over-promises. she's honest. 🫢


πŸ’œ REPLY πŸ’œ

RESP2

one of these:

  • valkey-protocol(7) Nil reply: when no element could be popped.
  • valkey-protocol(7) Array reply: a two-element array where the first element is the name of the key the elements were popped from, and the second element is an array of the popped elements. every entry in that elements array is also an array containing the member and its score. nesting, but make it fashion. ✨

RESP3

one of these:

  • valkey-protocol(7) Null reply: when no element could be popped.
  • valkey-protocol(7) Array reply: a two-element array where the first element is the name of the key the elements were popped from, and the second element is an array of the popped elements. every entry in that elements array is also an array containing the member and its score. same shape, different vibe. πŸ’–

πŸ”₯ COMPLEXITY πŸ”₯

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


πŸ‘‘ ACL CATEGORIES πŸ‘‘

@slow @sortedset @write


⭐ HISTORY ⭐

  • Available since: 7.0.0

πŸ’… 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), zrangebylex(3valkey), zrangebyscore(3valkey), zrangestore(3valkey), zrank(3valkey), zrem(3valkey), zremrangebylex(3valkey), zremrangebyrank(3valkey), zremrangebyscore(3valkey), zrevrange(3valkey), zrevrangebylex(3valkey), zrevrangebyscore(3valkey), zrevrank(3valkey), zscan(3valkey), zscore(3valkey), zunion(3valkey), zunionstore(3valkey)


and that's the whole page bestie 🫢 this one's from valkey 9.0.0, dated 2025-10-21 β€” go forth and pop responsibly, mwah πŸ’‹βœ¨
πŸ“¦ where this page comes from