{"id":28232,"date":"2022-12-25T11:37:31","date_gmt":"2022-12-25T01:37:31","guid":{"rendered":"https:\/\/database.guide\/?p=28232"},"modified":"2022-12-25T11:37:44","modified_gmt":"2022-12-25T01:37:44","slug":"redis-zrevrank-command-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/redis-zrevrank-command-explained\/","title":{"rendered":"Redis ZREVRANK Command Explained"},"content":{"rendered":"\n<p>In Redis, the <code>ZREVRANK<\/code> command returns the rank of the specified element of a sorted set ordered in descending order (from high to low).<\/p>\n\n\n\n<p>If we want the set to be in ascending order, we can use the <code><a href=\"https:\/\/database.guide\/redis-zrank-command-explained\/\" data-type=\"post\" data-id=\"28217\">ZRANK<\/a><\/code> command.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<p>The syntax goes like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZREVRANK key member<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Suppose we create a sorted set like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZADD scores 220 user:1 110 user:2 330 user:3 550 user:4 440 user:5 660 user:6<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 6<\/pre>\n\n\n\n<p>In my case the sorted set didn&#8217;t exist, and so it was created and the six elements were added as specified.<\/p>\n\n\n\n<p>Let&#8217;s now use the <code>ZREVRANK<\/code> command to find out the score of a given member:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZREVRANK scores user:3<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 3<\/pre>\n\n\n\n<p>In this case, the element has a rank of <code>3<\/code>. The rank is zero based, and so this element is ranked fourth, based on the set being in descending order.<\/p>\n\n\n\n<p>Let&#8217;s try another element:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZREVRANK scores user:6<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 0<\/pre>\n\n\n\n<p>This element has the highest score, and so we get zero. This is the lowest rank, based on the set being in descending order.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When the Element or Key Doesn&#8217;t Exist<\/h2>\n\n\n\n<p>If the element doesn&#8217;t exist we get <code>nil<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZREVRANK scores user:10<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(nil)<\/pre>\n\n\n\n<p>Same when the key doesn&#8217;t exist:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZREVRANK oops user:10<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(nil)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In Redis, the ZREVRANK command returns the rank of the specified element of a sorted set ordered in descending order (from high to low). If we want the set to be in ascending order, we can use the ZRANK command.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[153],"tags":[154,20],"class_list":["post-28232","post","type-post","status-publish","format-standard","hentry","category-redis","tag-commands","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/28232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=28232"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/28232\/revisions"}],"predecessor-version":[{"id":28253,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/28232\/revisions\/28253"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=28232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=28232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=28232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}