Upstash Documentation

LPOS

Find the position of an element.
2 min read

Use LPOS to find the position of an element in a list.

The scan starts at the head and, by default, reports the index of the first match or null when there is none. RANK selects which match to report: RANK 2 skips to the second occurrence, and a negative rank searches backwards from the tail, so RANK -1 finds the last occurrence. COUNT returns that many matching indexes instead of just one, and COUNT 0 returns all of them. MAXLEN limits how many elements are compared, which bounds the cost of the search on a long list at the price of possibly missing matches beyond that point.

It is the read-only way to locate a value before acting on it with LSET or LREM.

Syntax#

Arguments#

ArgumentRequiredRepeatableDescription
<key>YesNoRedis key targeted by the command.
<element>YesNoElement value to search for.
RANK <rank>NoNoWhich match to return; negative values search from the tail.
COUNT <num-matches>NoNoNumber of matches to return; 0 returns every match.
MAXLEN <len>NoNoMaximum number of entries to keep in the stream.

Response#

The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.

ProtocolReply
RESP2Null bulk string or null array, Integer, or array of integer positions
RESP3Null, Integer, or array of integer positions
Note

Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.

Examples#

TCP examples use the TLS REDIS_URL from the Upstash console. REST examples use UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN.

Redis CLI
@upstash/redis
upstash_redis
ioredis
node-redis
redis-py
go-redis
jedis
redis-rs