ZCOUNT(3valkey)
β¨ ZCOUNT β bestie, let's count some scores π
okay so this is a page from the Valkey Command Manual (manual section 3valkey, the one and only) and it's all about ZCOUNT, which is lowkey one of the most useful little sorted-set commands in existence. pull up a chair. π«Ά
πΈ NAME πΈ
zcount β returns the count of members in a sorted set that have scores within a range. that's it. that's the whole job. and she does it beautifully. β¨
π SYNOPSIS π
ZCOUNT key min max
π DESCRIPTION π
So basically: ZCOUNT returns the number of elements in the sorted set stored at key whose score falls between min and max. Inclusive-ish, range-y, gorgeous. π
The min and max arguments have the exact same semantics as described for ZRANGEBYSCORE β so whatever weird little syntax rules apply over there, they apply here too. no surprises, no cap.
Note: the command has a complexity of just O(log(N)) because it uses element ranks (see ZRANK) to get an idea of the range. Because of this there is no need to do a work proportional to the size of the range. π§
like... she literally does NOT scan the range. she peeks at the ranks and knows. main character energy fr fr. π₯
π REPLY π
valkey-protocol(7) integer reply: the number of members in the specified score range. just a number. a little integer. a cutie. π
π COMPLEXITY π
O(log(N)) with N being the number of elements in the sorted set. iconic behaviour honestly. β
π¦ ACL CATEGORIES π¦
@fast@read@sortedset
π HISTORY π
- Available since: 2.0.0 β she's an elder. a legend. been here since forever and still slaying. π
β¨ EXAMPLES β¨
watch her work:
127.0.0.1:6379> ZADD myzset 1 "one"
(integer) 1
127.0.0.1:6379> ZADD myzset 2 "two"
(integer) 1
127.0.0.1:6379> ZADD myzset 3 "three"
(integer) 1
127.0.0.1:6379> ZCOUNT myzset -inf +inf
(integer) 3
127.0.0.1:6379> ZCOUNT myzset (1 3
(integer) 2
ok so first we build the zset, then we count everything with -inf +inf and get 3, and then β and this is the part bestie β the (1 3 range is exclusive on the low end, so 1 gets left out and we get 2. the parenthesis is doing WORK. ππ
π SEE ALSO π
bzmpop(3valkey), bzpopmax(3valkey), bzpopmin(3valkey), zadd(3valkey), zcard(3valkey), zdiff(3valkey), zdiffstore(3valkey), zincrby(3valkey), zinter(3valkey), zintercard(3valkey), zinterstore(3valkey), zlexcount(3valkey), zmpop(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 version 9.0.0, dated 2025-10-21 β go forth and count responsibly. mwah. ππβ¨
π¦ where this page comes from
- fedora 44: valkey-doc 9.0.3-2.fc44 Β· CC-BY-SA-4.0
- fedora 44: valkey-doc 9.0.6-1.fc44 Β· CC-BY-SA-4.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms