In Redis, the ZMPOP command removes and returns one or more elements from the first non-empty sorted set in the provided list of keys.
The ZMPOP command was introduced in Redis 7.0.0.
Syntax
The syntax goes like this:
ZMPOP numkeys key [key ...] <MIN | MAX> [COUNT count]
Example
Suppose we create the following sorted set:
ZADD cats 1 Meow 2 Fluffy 3 Scratch 4 Purr 5 Bite 6 Bunting
Let’s use the ZMPOP command to pop an element from that sorted set:
ZMPOP 2 birds cats MIN
Result:
1) "cats"
2) 1) 1) "Meow"
2) "1"
Here I actually provided two sorted sets; birds and cats. The birds key didn’t exist and so it was treated as an empty sorted set. The cats key did exist with a sorted set, and so the operation was performed against that key.
In this case I specified MIN, which means that the elements with the lowest scores are popped. By default, only one element is popped and so the element with the lowest score was popped. Using MAX would’ve popped the element with the highest score.
Let’s take a look at the cats sorted set now:
ZRANGE cats 0 -1
Result:
1) "Fluffy" 2) "Scratch" 3) "Purr" 4) "Bite" 5) "Bunting"
We can see that Meow no longer exists in the sorted set.
Pop Multiple Elements
We can use the optional COUNT argument to pop multiple elements. By default, this is set to 1 but we can specify a different number.
Here’s an example of using the COUNT argument:
ZMPOP 1 cats MIN COUNT 2
Result:
1) "cats"
2) 1) 1) "Fluffy"
2) "2"
2) 1) "Scratch"
2) "3"
This time two elements were popped because I specified a count of 2.
I also removed the birds key from the list of keys and so I changed the first argument to 1 instead of 2.
Let’s take a look at the cats sorted set now:
ZRANGE cats 0 -1
Result:
1) "Purr" 2) "Bite" 3) "Bunting"
Now there’s only three elements left, because the others have been popped.
When COUNT is Greater than the Sorted Set’s Cardinality
If the COUNT argument is a larger number than the number of elements in the sorted set, then all elements are popped.
Example:
ZMPOP 1 cats MAX COUNT 10
Result:
1) "cats"
2) 1) 1) "Bunting"
2) "6"
2) 1) "Bite"
2) "5"
3) 1) "Purr"
2) "4"
We can see that all elements were popped from our sorted set. In this case I used the MAX argument and so it popped the elements with the highest scores. However, given we used a COUNT that was greater than the sorted set’s cardinality, all elements were popped.
When None of the Keys Exist
If none of the keys we pass actually exist, we get nil:
ZMPOP 2 oops1 oops2 MIN
Result:
(nil)
Wrong Data Type
When the key contains the wrong data type, an error is returned:
ZMPOP 1 countries MIN
Result:
(error) WRONGTYPE Operation against a key holding the wrong kind of value
In this case, the countries key contains a set (not a sorted set), and so we got an error.
We can use the TYPE command to check the key’s data type:
TYPE countries
Result:
set
As suspected, it’s a set.