Redis Command: LPOS

Summary

Command NameLPOS
UsageGet list item index
Group list
ACL Category@read
@list
@slow
Time ComplexityO(N)
** N is the number of list items
FlagREADONLY
Arity-3

Notes

  • Time complexity of this is O(N), in the average case.
  • If the item is very close to the HEAD/TAIL then the command will run in constant time.
  • If the MAXLEN option is provided then the time complexity will be in constant time.

Signature

LPOS <key> <item_to_find> [ Rank <rank> ] [ COUNT <count> ] [ MAXLEN <maxlen> ]

Usage

Get the index of item(s) in the list, which matches the provided item. Operation can be performed starting from the HEAD or from the TAIL. This command is useful for checking the position/index of an item in the list, and also useful for checking the existence of an item in the list.

Notes

  • Index of the list item is Zero based. The first item is at index Zero(0).
  • If the command does not return (nil). Instead, return some index value (can be zero) which means the item exists in the list.

Arguments

ParameterDescriptionNameTypeOptional
<key>Key name of the listkeykey
<item_to_find>The item to findelementstring
<rank>Return the <rank>th occurrence that matches the <item_to_find>rankintegerTrue
<count>Return the indexes of <count> number of occurrencesnum-matchesintegerTrue
<maxlen>How many maximum number of list items to check(go through) for the item lenintegerTrue

Notes

  • If the COUNT option is passed as 0(zero), then that means no limit to the result. All the positions will be returned in that case.
  • Rank options means – start from the <rank>th occurrence of the item. i.e. if rank is 2 and the count is 3 then it will start considering the index which is the 2nd occurrence of the item, and it will return indexed up to a maximum of 3.
  • <rank> can be a negative value.
  • <count> and <maxlen> can not be negative, these should be positive values.

Return Value

Return valueCase for the return valueType
Index of matching itemOn successful execution of the command the item was foundinteger
List of indexes of matching itemIf COUNT option is provided and the command is successfularray[integer]
(nil)If the item is not found in the listnull
errorIf the key exists but the data type of the key is not a listerror

Notes

  • If the RANK param is negative then the operation is performed from the TAIL/Right of the list and moves to the HEAD. To get the last occurrence of the item use the command like below-
    lpos bigboxlist myitem rank -1
  • If key is not of type list, the following error message is returned-
    (error) WRONGTYPE Operation against a key holding the wrong kind of value
  • If <count> is negative then the following error is returned-
    (error) ERR COUNT can’t be negative
  • If <maxlen> is negative then the following error is returned-
    (error) ERR MAXLEN can’t be negative

Command Examples

Here are a few examples of the LPOS command usage-

# Redis LPOS command examples

# Push items to list
127.0.0.1:6379> rpush bigboxlist one two three four five one testA two testB testC one two nine one nine
(integer) 15

# Check list items
127.0.0.1:6379> lrange bigboxlist 0 -1
 1) "one"
 2) "two"
 3) "three"
 4) "four"
 5) "five"
 6) "one"
 7) "testA"
 8) "two"
 9) "testB"
10) "testC"
11) "one"
12) "two"
13) "nine"
14) "one"
15) "nine"

# Check first index of "one"
127.0.0.1:6379> lpos bigboxlist one
(integer) 0

# Check first index of "two"
127.0.0.1:6379> lpos bigboxlist two
(integer) 1

# Check first index of "five"
127.0.0.1:6379> lpos bigboxlist five
(integer) 4

# Check second occurrence of "one"
127.0.0.1:6379> lpos bigboxlist one rank 2
(integer) 5

# Check 5th occurrence of "one"
# We get (nil) as this item occurs less than 5 times
127.0.0.1:6379> lpos bigboxlist one rank 5
(nil)

# Get first 2 occurrance of "one"
127.0.0.1:6379> lpos bigboxlist one count 2
1) (integer) 0
2) (integer) 5

# Try to get first 8 occurrences of "one"
# We get only 4 indexes, as "one" is there only 4 times in the list
127.0.0.1:6379> lpos bigboxlist one count 8
1) (integer) 0
2) (integer) 5
3) (integer) 10
4) (integer) 13

# Pass count as 0 to return all occurrences
127.0.0.1:6379> lpos bigboxlist one count 0
1) (integer) 0
2) (integer) 5
3) (integer) 10
4) (integer) 13

# Get 2 occurrences of "one" starting from the 2nd occurrance
127.0.0.1:6379> lpos bigboxlist one rank 2 count 2
1) (integer) 5
2) (integer) 10

# Get all occurrences of "one" starting from the 2nd occurrance
127.0.0.1:6379> lpos bigboxlist one rank 2 count 0
1) (integer) 5
2) (integer) 10
3) (integer) 13

# Get one occurence from the end of the list
127.0.0.1:6379> lpos bigboxlist one rank -1
(integer) 13

# Get 3 occurences of "one" from the end
127.0.0.1:6379> lpos bigboxlist one rank -1 count 3
1) (integer) 13
2) (integer) 10
3) (integer) 5

# Try to get index of "two" withing first 1 item
# (nil) is returned as "two" is not there is first 1 item
127.0.0.1:6379> lpos bigboxlist two maxlen 1
(nil)

# Get index of "two" withing first 10 list items
# We get the index 1, as this is the first occurence
127.0.0.1:6379> lpos bigboxlist two maxlen 10
(integer) 1

# Get 2 occurrences of "two" withing first 10 items
127.0.0.1:6379> lpos bigboxlist two count 2 maxlen 10
1) (integer) 1
2) (integer) 7

# Get all occurrences of "two" withing first 10 items
127.0.0.1:6379> lpos bigboxlist two count 0 maxlen 10
1) (integer) 1
2) (integer) 7

# Get all occurrences of "two" withing first 15 items
127.0.0.1:6379> lpos bigboxlist two count 0 maxlen 15
1) (integer) 1
2) (integer) 7
3) (integer) 11

# Get results from the end and consider 10 items from the end
127.0.0.1:6379> lpos bigboxlist two maxlen 10 rank -1
(integer) 11

# Get 2nd occurence from the end and consider 10 items from the end
127.0.0.1:6379> lpos bigboxlist two maxlen 10 rank -2
(integer) 7

# Get 1st occurence of "three" from the end and consider 10 items from the end
# Three does not exist in last 10 items, so we get (nil)
127.0.0.1:6379> lpos bigboxlist three maxlen 10 rank -1
(nil)

# Try to get a non existing element from a list
# We get (nil) value
127.0.0.1:6379> lpos bigboxlist nonexistingitem
(nil)

# Set a string value
127.0.0.1:6379> set mystr "my string value here"
OK

# Try to use LPOS command on a string
# We get an error for the wrong type of operation
127.0.0.1:6379> lpos mystr m
(error) WRONGTYPE Operation against a key holding the wrong kind of value

# Error returned if COUNT is negative
127.0.0.1:6379> lpos bigboxlist one count -3
(error) ERR COUNT can't be negative

# Error returned if MAXLEN is negative
127.0.0.1:6379> lpos bigboxlist one maxlen -3
(error) ERR MAXLEN can't be negative

Notes

  • If RANK is negative then the result indexes are in descending order.

Code Implementations

Here are the usage examples of the Redis LPOS command in different programming languages.

// Redis LPOS command example in Golang

package main

import (
	"context"
	"fmt"

	"github.com/redis/go-redis/v9"
)

var rdb *redis.Client
var ctx context.Context

func init() {
	rdb = redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Username: "default",
		Password: "",
		DB:       0,
	})

	ctx = context.Background()
}

func main() {

	// Push items to list
	// Command: rpush bigboxlist one two three four five one testA two testB testC one two nine one nine
	// Result: (integer) 15
	listPushResult, err := rdb.RPush(ctx, "bigboxlist", "one", "two", "three", "four", "five", "one", "testA", "two", "testB", "testC", "one", "two", "nine", "one", "nine").Result()

	if err != nil {
		fmt.Println("Command: rpush bigboxlist one two three four five one testA two testB testC one two nine one nine | Error: " + err.Error())
	}

	fmt.Printf("Command: rpush bigboxlist one two three four five one testA two testB testC one two nine one nine | Result: %vn", listPushResult)

	// Check list items
	// Command: lrange bigboxlist 0 -1
	// Result:
	//      1) "one"
	//      2) "two"
	//      3) "three"
	//      4) "four"
	//      5) "five"
	//      6) "one"
	//      7) "testA"
	//      8) "two"
	//      9) "testB"
	//      10) "testC"
	//      11) "one"
	//      12) "two"
	//      13) "nine"
	//      14) "one"
	//      15) "nine"
	lrangeResult, err := rdb.LRange(ctx, "bigboxlist", 0, -1).Result()

	if err != nil {
		fmt.Println("Command: lrange bigboxlist 0 -1 | Error: " + err.Error())
	}

	fmt.Println("Command: lrange bigboxlist 0 -1 | Result:")

	for _, item := range lrangeResult {
		fmt.Println(item)
	}

	// Check first index of "one"
	// Command: lpos bigboxlist one
	// Result: (integer) 0
	lposResult, err := rdb.LPos(ctx, "bigboxlist", "one", redis.LPosArgs{}).Result()

	if err != nil {
		fmt.Println("Command: lpos bigboxlist one | Error: " + err.Error())
	}

	fmt.Printf("Command: lpos bigboxlist one | Result: %vn", lposResult)

	// Check first index of "two"
	// Command: lpos bigboxlist two
	// Result: (integer) 1
	lposResult, err = rdb.LPos(ctx, "bigboxlist", "two", redis.LPosArgs{}).Result()

	if err != nil {
		fmt.Println("Command: lpos bigboxlist two | Error: " + err.Error())
	}

	fmt.Printf("Command: lpos bigboxlist two | Result: %vn", lposResult)

	// Check first index of "five"
	// Command: lpos bigboxlist five
	// Result: (integer) 4
	lposResult, err = rdb.LPos(ctx, "bigboxlist", "five", redis.LPosArgs{}).Result()

	if err != nil {
		fmt.Println("Command: lpos bigboxlist five | Error: " + err.Error())
	}

	fmt.Println("Command: lpos bigboxlist five | Result:", lposResult)

	// Check second occurrence of "one"
	//
	// Command: lpos bigboxlist one rank 2
	// Result: (integer) 5
	lposResult, err = rdb.LPos(ctx, "bigboxlist", "one", redis.LPosArgs{Rank: 2}).Result()

	if err != nil {
		fmt.Println("Command: lpos bigboxlist one rank 2 | Error: " + err.Error())
	}

	fmt.Println("Command: lpos bigboxlist one rank 2 | Result:", lposResult)

	// Check 5th occurrence of "one"
	// We get (nil) as this item occurs less than 5 times
	// Command: lpos bigboxlist one rank 5
	// Result: (nil)
	lposResult, err = rdb.LPos(ctx, "bigboxlist", "one", redis.LPosArgs{Rank: 5}).Result()

	if err != nil {
		fmt.Println("Command: lpos bigboxlist one rank 5 | Error: " + err.Error())
	}

	fmt.Printf("Command: lpos bigboxlist one rank 5 | Result: %vn", lposResult)

	// Get first 2 occurrance of "one"
	// Command: lpos bigboxlist one count 2
	// Result:
	//      1) (integer) 0
	//      2) (integer) 5
	lposResults, err := rdb.LPosCount(ctx, "bigboxlist", "one", 2, redis.LPosArgs{}).Result()

	if err != nil {
		fmt.Println("Command: lpos bigboxlist one count 2 | Error: " + err.Error())
	}

	fmt.Println("Command: lpos bigboxlist one count 2 | Result: ", lposResults)

	// Try to get first 8 occurrences of "one"             //
	// We get only 4 indexes, as "one" is there only 4 times in the list
	// Command: lpos bigboxlist one count 8
	// Result:
	//      1) (integer) 0
	//      2) (integer) 5
	//      3) (integer) 10
	//      4) (integer) 13
	lposResults, err = rdb.LPosCount(ctx, "bigboxlist", "one", 8, redis.LPosArgs{}).Result()

	if err != nil {
		fmt.Println("Command: lpos bigboxlist one count 8 | Error: " + err.Error())
	}

	fmt.Println("Command: lpos bigboxlist one count 8 | Result: ", lposResults)

	// Pass count as 0 to return all occurrences
	// Command: lpos bigboxlist one count 0
	// Result:
	//      1) (integer) 0
	//      2) (integer) 5
	//      3) (integer) 10
	//      4) (integer) 13
	lposResults, err = rdb.LPosCount(ctx, "bigboxlist", "one", 0, redis.LPosArgs{}).Result()

	if err != nil {
		fmt.Println("Command: lpos bigboxlist one count 0 | Error: " + err.Error())
	}

	fmt.Println("Command: lpos bigboxlist one count 0 | Result: ", lposResults)

	// Get 2 occurrences of "one" starting from the 2nd occurrance
	// Command: lpos bigboxlist one rank 2 count 2
	// Result:
	//      1) (integer) 5
	//      2) (integer) 10
	lposResults, err = rdb.LPosCount(ctx, "bigboxlist", "one", 2, redis.LPosArgs{Rank: 2}).Result()

	if err != nil {
		fmt.Println("Command: lpos bigboxlist one rank 2 count 2 | Error: " + err.Error())
	}

	fmt.Println("Command: lpos bigboxlist one rank 2 count 2 | Result: ", lposResults)

	// Get all occurrences of "one" starting from the 2nd occurrance
	// Command: lpos bigboxlist one rank 2 count 0
	// Result:
	//      1) (integer) 5
	//      2) (integer) 10
	//      3) (integer) 13
	lposResults, err = rdb.LPosCount(ctx, "bigboxlist", "one", 0, redis.LPosArgs{Rank: 2}).Result()

	if err != nil {
		fmt.Println("Command: lpos bigboxlist one rank 2 count 0 | Error: " + err.Error())
	}

	fmt.Println("Command: lpos bigboxlist one rank 2 count 0 | Result: ", lposResults)

	// Get one occurence from the end of the list
	// Command: lpos bigboxlist one rank -1
	// Result: (integer) 13
	lposResult, err = rdb.LPos(ctx, "bigboxlist", "one", redis.LPosArgs{Rank: -1}).Result()

	if err != nil {
		fmt.Println("Command: lpos bigboxlist one rank -1 | Error: " + err.Error())
	}

	fmt.Printf("Command: lpos bigboxlist one rank -1 | Result: %vn", lposResult)

	// Get 3 occurences of "one" from the end
	// Command: lpos bigboxlist one rank -1 count 3
	// Result:
	//      1) (integer) 13
	//      2) (integer) 10
	//      3) (integer) 5
	lposResults, err = rdb.LPosCount(ctx, "bigboxlist", "one", 3, redis.LPosArgs{Rank: -1}).Result()

	if err != nil {
		fmt.Println("Command: lpos bigboxlist one rank -1 count 3 | Error: " + err.Error())
	}

	fmt.Println("Command: lpos bigboxlist one rank -1 count 3 | Result: ", lposResults)

	// Try to get index of "two" withing first 1 item
	// (nil) is returned as "two" is not there is first 1 item
	// Command: lpos bigboxlist two maxlen 1
	// Result: (nil)
	lposResult, err = rdb.LPos(ctx, "bigboxlist", "two", redis.LPosArgs{MaxLen: 1}).Result()

	if err != nil {
		fmt.Println("Command: lpos bigboxlist two maxlen 1 | Error: " + err.Error())
	}

	fmt.Printf("Command: lpos bigboxlist two maxlen 1 | Result: %vn", lposResult)

	// Get index of "two" withing first 10 list items
	// We get the index 1, as this is the first occurence
	// Command: lpos bigboxlist two maxlen 10
	// Result: (integer) 1
	lposResult, err = rdb.LPos(ctx, "bigboxlist", "two", redis.LPosArgs{MaxLen: 10}).Result()

	if err != nil {
		fmt.Println("Command: lpos bigboxlist two maxlen 10 | Error: " + err.Error())
	}

	fmt.Printf("Command: lpos bigboxlist two maxlen 10 | Result: %vn", lposResult)

	// Get 2 occurrences of "two" withing first 10 items
	// Command: lpos bigboxlist two count 2 maxlen 10
	// Result:
	//      1) (integer) 1
	//      2) (integer) 7
	lposResults, err = rdb.LPosCount(ctx, "bigboxlist", "two", 2, redis.LPosArgs{MaxLen: 10}).Result()

	if err != nil {
		fmt.Println("Command: lpos bigboxlist two count 2 maxlen 10 | Error: " + err.Error())
	}

	fmt.Println("Command: lpos bigboxlist two count 2 maxlen 10 | Result: ", lposResults)

	// Get all occurrences of "two" withing first 10 items
	// Command: lpos bigboxlist two count 0 maxlen 10
	// Result:
	//      1) (integer) 1
	//      2) (integer) 7
	lposResults, err = rdb.LPosCount(ctx, "bigboxlist", "two", 0, redis.LPosArgs{MaxLen: 10}).Result()

	if err != nil {
		fmt.Println("Command: lpos bigboxlist two count 0 maxlen 10 | Error: " + err.Error())
	}

	fmt.Println("Command: lpos bigboxlist two count 0 maxlen 10 | Result: ", lposResults)

	// Get all occurrences of "two" withing first 15 items
	// Command: lpos bigboxlist two count 0 maxlen 15
	// Result:
	//      1) (integer) 1
	//      2) (integer) 7
	//      3) (integer) 11
	lposResults, err = rdb.LPosCount(ctx, "bigboxlist", "two", 0, redis.LPosArgs{MaxLen: 15}).Result()

	if err != nil {
		fmt.Println("Command: lpos bigboxlist two count 0 maxlen 15 | Error: " + err.Error())
	}

	fmt.Println("Command: lpos bigboxlist two count 0 maxlen 15 | Result: ", lposResults)

	// Get results from the end and consider 10 items from the end
	// Command: lpos bigboxlist two maxlen 10 rank -1
	// Result: (integer) 11
	lposResult, err = rdb.LPos(ctx, "bigboxlist", "two", redis.LPosArgs{MaxLen: 10, Rank: -1}).Result()

	if err != nil {
		fmt.Println("Command: lpos bigboxlist two maxlen 10 rank -1 | Error: " + err.Error())
	}

	fmt.Println("Command: lpos bigboxlist two maxlen 10 rank -1 | Result: ", lposResult)

	// Get 2nd occurence from the end and consider 10 items from the end
	// Command: lpos bigboxlist two maxlen 10 rank -2
	// Result: (integer) 7
	lposResult, err = rdb.LPos(ctx, "bigboxlist", "two", redis.LPosArgs{MaxLen: 10, Rank: -2}).Result()

	if err != nil {
		fmt.Println("Command: lpos bigboxlist two maxlen 10 rank -2 | Error: " + err.Error())
	}

	fmt.Println("Command: lpos bigboxlist two maxlen 10 rank -2 | Result: ", lposResult)

	// Get 1st occurence of "three" from the end and consider 10 items from the end
	// Three does not exist in last 10 items, so we get (nil)
	// Command: lpos bigboxlist three maxlen 10 rank -1
	// Result: (nil)
	lposResult, err = rdb.LPos(ctx, "bigboxlist", "three", redis.LPosArgs{MaxLen: 10, Rank: -1}).Result()

	if err != nil {
		fmt.Println("Command: lpos bigboxlist three maxlen 10 rank -1 | Error: " + err.Error())
	}

	fmt.Println("Command: lpos bigboxlist three maxlen 10 rank -1 | Result: ", lposResult)

	// Try to get a non existing element from a list
	// We get (nil) value
	// Command: lpos bigboxlist nonexistingitem
	// Result: (nil)
	lposResult, err = rdb.LPos(ctx, "bigboxlist", "nonexistingitem", redis.LPosArgs{}).Result()

	if err != nil {
		fmt.Println("Command: lpos bigboxlist nonexistingitem | Error: " + err.Error())
	}

	fmt.Println("Command: lpos bigboxlist nonexistingitem | Result: " , lposResult)

	// Set a string value
	// Command: set mystr "my string value here"
	// Result: OK
	setResult, err := rdb.Set(ctx, "mystr", "my string value here", 0).Result()

	if err != nil {
		fmt.Println("Command: set mystr "my string value here" | Error: " + err.Error())
	}

	fmt.Println("Command: set mystr "my string value here" | Result: " + setResult)

	// Try to use LPOS command on a string
	// We get an error for the wrong type of operation
	// Command: lpos mystr m
	// Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
	lposResult, err = rdb.LPos(ctx, "mystr", "m", redis.LPosArgs{}).Result()

	if err != nil {
		fmt.Println("Command: lpos mystr m | Error: " + err.Error())
	}

	fmt.Println("Command: lpos mystr m | Result: ", lposResult)

	// Error returned if COUNT is negative
	// Command: lpos bigboxlist one count -3
	// Result: (error) ERR COUNT can't be negative
	lposResults, err = rdb.LPosCount(ctx, "bigboxlist", "one", -3, redis.LPosArgs{}).Result()

	if err != nil {
		fmt.Println("Command: lpos bigboxlist one count -3 | Error: " + err.Error())
	}

	fmt.Println("Command: lpos bigboxlist one count -3 | Result: ", lposResults)

	// Error returned if MAXLEN is negative
	// Command: lpos bigboxlist one maxlen -3
	// Result: (error) ERR MAXLEN can't be negative
	lposResult, err = rdb.LPos(ctx, "bigboxlist", "one", redis.LPosArgs{MaxLen: -3}).Result()

	if err != nil {
		fmt.Println("Command: lpos bigboxlist one maxlen -3 | Error: " + err.Error())
	}

	fmt.Println("Command: lpos bigboxlist one maxlen -3 | Result: ", lposResult)

}

Output:

Command: rpush bigboxlist one two three four five one testA two testB testC one two nine one nine | Result: 15
Command: lrange bigboxlist 0 -1 | Result:
one
two
three
four
five
one
testA
two
testB
testC
one
two
nine
one
nine

Command: lpos bigboxlist one | Result: 0
Command: lpos bigboxlist two | Result: 1
Command: lpos bigboxlist five | Result: 4
Command: lpos bigboxlist one rank 2 | Result: 5
Command: lpos bigboxlist one rank 5 | Error: redis: nil
Command: lpos bigboxlist one rank 5 | Result: 0
Command: lpos bigboxlist one count 2 | Result:  [0 5]
Command: lpos bigboxlist one count 8 | Result:  [0 5 10 13]
Command: lpos bigboxlist one count 0 | Result:  [0 5 10 13]
Command: lpos bigboxlist one rank 2 count 2 | Result:  [5 10]
Command: lpos bigboxlist one rank 2 count 0 | Result:  [5 10 13]
Command: lpos bigboxlist one rank -1 | Result: 13
Command: lpos bigboxlist one rank -1 count 3 | Result:  [13 10 5]
Command: lpos bigboxlist two maxlen 1 | Error: redis: nil
Command: lpos bigboxlist two maxlen 1 | Result: 0
Command: lpos bigboxlist two maxlen 10 | Result: 1
Command: lpos bigboxlist two count 2 maxlen 10 | Result:  [1 7]
Command: lpos bigboxlist two count 0 maxlen 10 | Result:  [1 7]
Command: lpos bigboxlist two count 0 maxlen 15 | Result:  [1 7 11]
Command: lpos bigboxlist two maxlen 10 rank -1 | Result:  11
Command: lpos bigboxlist two maxlen 10 rank -2 | Result:  7
Command: lpos bigboxlist three maxlen 10 rank -1 | Error: redis: nil
Command: lpos bigboxlist three maxlen 10 rank -1 | Result:  0

Command: lpos bigboxlist nonexistingitem | Error: redis: nil
Command: lpos bigboxlist nonexistingitem | Result:  0

Command: set mystr "my string value here" | Result: OK
Command: lpos mystr m | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: lpos mystr m | Result:  0

Command: lpos bigboxlist one count -3 | Error: ERR COUNT can't be negative
Command: lpos bigboxlist one count -3 | Result:  []

Command: lpos bigboxlist one maxlen -3 | Error: ERR MAXLEN can't be negative
Command: lpos bigboxlist one maxlen -3 | Result:  0

Notes

  • Use “LPos” or “LPosCount” method from redis-go module.
  • Signature of “LPos” methods is-
    func (c cmdable) LPos(ctx context.Context, key string, value string, a LPosArgs) *IntCmd
  • Signature of “LPosCount” method is-
    func (c cmdable) LPosCount(ctx context.Context, key string, value string, count int64, a LPosArgs) *IntSliceCmd
  • Definition of LPosArgs-
    type LPosArgs struct {
        Rank, MaxLen int64
    }
// Redis LPOS command example in JavaScript(NodeJS)

import { createClient } from "redis";

// Create redis client
const redisClient = createClient({
  url: "redis://default:@localhost:6379",
});

await redisClient.on("error", (err) =>
  console.log("Error while connecting to Redis", err)
);

// Connect Redis client
await redisClient.connect();

/**
 * Push items to list
 *
 * Command: rpush bigboxlist one two three four five one testA two testB testC one two nine one nine
 * Result: (integer) 15
 */
let commandResult = await redisClient.rPush("bigboxlist", [
  "one",
  "two",
  "three",
  "four",
  "five",
  "one",
  "testA",
  "two",
  "testB",
  "testC",
  "one",
  "two",
  "nine",
  "one",
  "nine",
]);

console.log(
  "Command: rpush bigboxlist one two three four five one testA two testB testC one two nine one nine | Result:" +
    commandResult
);

/**
 * Check list items
 *
 * Command: lrange bigboxlist 0 -1
 * Result:
 *      1) "one"
 *      2) "two"
 *      3) "three"
 *      4) "four"
 *      5) "five"
 *      6) "one"
 *      7) "testA"
 *      8) "two"
 *      9) "testB"
 *      10) "testC"
 *      11) "one"
 *      12) "two"
 *      13) "nine"
 *      14) "one"
 *      15) "nine"
 */
commandResult = await redisClient.lRange("bigboxlist", 0, -1);

console.log("Command: lrange bigboxlist 0 -1 | Result:" + commandResult);

/**
 * Check first index of "one"
 *
 * Command: lpos bigboxlist one
 * Result: (integer) 0
 */
commandResult = await redisClient.lPos("bigboxlist", "one");

console.log("Command: lpos bigboxlist one | Result: " + commandResult);

/**
 * Check first index of "two"
 *
 * Command: lpos bigboxlist two
 * Result: (integer) 1
 */
commandResult = await redisClient.lPos("bigboxlist", "two");

console.log("Command: lpos bigboxlist two | Result: " + commandResult);

/**
 * Check first index of "five"
 *
 * Command: lpos bigboxlist five
 * Result: (integer) 4
 */
commandResult = await redisClient.lPos("bigboxlist", "five");

console.log("Command: lpos bigboxlist five | Result: " + commandResult);

/**
 * Check second occurrence of "one"
 *
 * Command: lpos bigboxlist one rank 2
 * Result: (integer) 5
 */
commandResult = await redisClient.lPos("bigboxlist", "one", { RANK: 2 });

console.log("Command: lpos bigboxlist one rank 2 | Result: " + commandResult);

/**
 * Check 5th occurrence of "one"
 * We get (nil) as this item occurs less than 5 times
 *
 * Command: lpos bigboxlist one rank 5
 * Result: (nil)
 */
commandResult = await redisClient.lPos("bigboxlist", "one", { RANK: 5 });

console.log("Command: lpos bigboxlist one rank 5 | Result: " + commandResult);

/**
 * Get first 2 occurrance of "one"
 *
 * Command: lpos bigboxlist one count 2
 * Result:
 *      1) (integer) 0
 *      2) (integer) 5
 */
commandResult = await redisClient.lPosCount("bigboxlist", "one", 2);

console.log("Command: lpos bigboxlist one count 2 | Result: " + commandResult);

/**
 * Try to get first 8 occurrences of "one"             *
 * We get only 4 indexes, as "one" is there only 4 times in the list
 *
 * Command: lpos bigboxlist one count 8
 * Result:
 *      1) (integer) 0
 *      2) (integer) 5
 *      3) (integer) 10
 *      4) (integer) 13
 */
commandResult = await redisClient.lPosCount("bigboxlist", "one", 8);

console.log("Command: lpos bigboxlist one count 8 | Result: " + commandResult);

/**
 * Pass count as 0 to return all occurrences
 *
 * Command: lpos bigboxlist one count 0
 * Result:
 *      1) (integer) 0
 *      2) (integer) 5
 *      3) (integer) 10
 *      4) (integer) 13
 */
commandResult = await redisClient.lPosCount("bigboxlist", "one", 0);

console.log("Command: lpos bigboxlist one count 0 | Result: " + commandResult);

/**
 * Get 2 occurrences of "one" starting from the 2nd occurrance
 *
 * Command: lpos bigboxlist one rank 2 count 2
 * Result:
 *      1) (integer) 5
 *      2) (integer) 10
 */
commandResult = await redisClient.lPosCount("bigboxlist", "one", 2, {
  RANK: 2,
});

console.log(
  "Command: lpos bigboxlist one rank 2 count 2 | Result: " + commandResult
);

/**
 * Get all occurrences of "one" starting from the 2nd occurrance
 *
 * Command: lpos bigboxlist one rank 2 count 0
 * Result:
 *      1) (integer) 5
 *      2) (integer) 10
 *      3) (integer) 13
 */
commandResult = await redisClient.lPosCount("bigboxlist", "one", 0, {
  RANK: 2,
});

console.log(
  "Command: lpos bigboxlist one rank 2 count 0 | Result: " + commandResult
);

/**
 * Get one occurence from the end of the list
 *
 * Command: lpos bigboxlist one rank -1
 * Result: (integer) 13
 */
commandResult = await redisClient.lPos("bigboxlist", "one", { RANK: -1 });

console.log("Command: lpos bigboxlist one rank -1 | Result: " + commandResult);

/**
 * Get 3 occurences of "one" from the end
 *
 * Command: lpos bigboxlist one rank -1 count 3
 * Result:
 *      1) (integer) 13
 *      2) (integer) 10
 *      3) (integer) 5
 */
commandResult = await redisClient.lPosCount("bigboxlist", "one", 3, {
  RANK: -1,
});

console.log(
  "Command: lpos bigboxlist one rank -1 count 3 | Result: " + commandResult
);

/**
 * Try to get index of "two" withing first 1 item
 * (nil) is returned as "two" is not there is first 1 item
 *
 * Command: lpos bigboxlist two maxlen 1
 * Result: (nil)
 */
commandResult = await redisClient.lPos("bigboxlist", "two", { MAXLEN: 1 });

console.log("Command: lpos bigboxlist two maxlen 1 | Result: " + commandResult);

/**
 * Get index of "two" withing first 10 list items
 * We get the index 1, as this is the first occurence
 *
 * Command: lpos bigboxlist two maxlen 10
 * Result: (integer) 1
 */
commandResult = await redisClient.lPos("bigboxlist", "two", { MAXLEN: 10 });

console.log(
  "Command: lpos bigboxlist two maxlen 10 | Result: " + commandResult
);

/**
 * Get 2 occurrences of "two" withing first 10 items
 *
 * Command: lpos bigboxlist two count 2 maxlen 10
 * Result:
 *      1) (integer) 1
 *      2) (integer) 7
 */
commandResult = await redisClient.lPosCount("bigboxlist", "two", 2, {
  MAXLEN: 10,
});

console.log(
  "Command: lpos bigboxlist two count 2 maxlen 10 | Result: " + commandResult
);

/**
 * Get all occurrences of "two" withing first 10 items
 *
 * Command: lpos bigboxlist two count 0 maxlen 10
 * Result:
 *      1) (integer) 1
 *      2) (integer) 7
 */
commandResult = await redisClient.lPosCount("bigboxlist", "two", 0, {
  MAXLEN: 10,
});

console.log(
  "Command: lpos bigboxlist two count 0 maxlen 10 | Result: " + commandResult
);

/**
 * Get all occurrences of "two" withing first 15 items
 *
 * Command: lpos bigboxlist two count 0 maxlen 15
 * Result:
 *      1) (integer) 1
 *      2) (integer) 7
 *      3) (integer) 11
 */
commandResult = await redisClient.lPosCount("bigboxlist", "two", 0, {
  MAXLEN: 15,
});

console.log(
  "Command: lpos bigboxlist two count 0 maxlen 15 | Result: " + commandResult
);

/**
 * Get results from the end and consider 10 items from the end
 *
 * Command: lpos bigboxlist two maxlen 10 rank -1
 * Result: (integer) 11
 */
commandResult = await redisClient.lPos("bigboxlist", "two", {
  MAXLEN: 10,
  RANK: -1,
});

console.log(
  "Command: lpos bigboxlist two maxlen 10 rank -1 | Result: " + commandResult
);

/**
 * Get 2nd occurence from the end and consider 10 items from the end
 *
 * Command: lpos bigboxlist two maxlen 10 rank -2
 * Result: (integer) 7
 */
commandResult = await redisClient.lPos("bigboxlist", "two", {
  MAXLEN: 10,
  RANK: -2,
});

console.log(
  "Command: lpos bigboxlist two maxlen 10 rank -2 | Result: " + commandResult
);

/**
 * Get 1st occurence of "three" from the end and consider 10 items from the end
 * Three does not exist in last 10 items, so we get (nil)
 *
 * Command: lpos bigboxlist three maxlen 10 rank -1
 * Result: (nil)
 */
commandResult = await redisClient.lPos("bigboxlist", "three", {
  MAXLEN: 10,
  RANK: -1,
});

console.log(
  "Command: lpos bigboxlist three maxlen 10 rank -1 | Result: " + commandResult
);

/**
 * Try to get a non existing element from a list
 * We get (nil) value
 *
 * Command: lpos bigboxlist nonexistingitem
 * Result: (nil)
 */
commandResult = await redisClient.lPos("bigboxlist", "nonexistingitem");

console.log(
  "Command: lpos bigboxlist nonexistingitem | Result: " + commandResult
);

/**
 * Set a string value
 *
 * Command: set mystr "my string value here"
 * Result: OK
 */
commandResult = await redisClient.set("mystr", "my string value here");

console.log(
  'Command: set mystr "my string value here" | Result: ' + commandResult
);

/**
 * Try to use LPOS command on a string
 * We get an error for the wrong type of operation
 *
 * Command: lpos mystr m
 * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
 */
try {
  commandResult = await redisClient.lPos("mystr", "m");

  console.log("Command: lpos mystr m | Result: " + commandResult);
} catch (err) {
  console.log("Command: lpos mystr m | Error: ", err);
}

/**
 * Error returned if COUNT is negative
 *
 * Command: lpos bigboxlist one count -3
 * Result: (error) ERR COUNT can't be negative
 */
try {
  commandResult = await redisClient.lPosCount("bigboxlist", "one", -3);

  console.log(
    "Command: lpos bigboxlist one count -3 | Result: " + commandResult
  );
} catch (err) {
  console.log("Command: lpos bigboxlist one count -3 | Error: ", err);
}

/**
 * Error returned if MAXLEN is negative
 *
 * Command: lpos bigboxlist one maxlen -3
 * Result: (error) ERR MAXLEN can't be negative
 */
try {
  commandResult = await redisClient.lPos("bigboxlist", "one", { MAXLEN: -3 });

  console.log(
    "Command: lpos bigboxlist one maxlen -3 | Result: " + commandResult
  );
} catch (err) {
  console.log("Command: lpos bigboxlist one maxlen -3 | Error: ", err);
}

process.exit(0);

Output:

Command: rpush bigboxlist one two three four five one testA two testB testC one two nine one nine | Result:15

Command: lrange bigboxlist 0 -1 | Result:one,two,three,four,five,one,testA,two,testB,testC,one,two,nine,one,nine

Command: lpos bigboxlist one | Result: 0
Command: lpos bigboxlist two | Result: 1
Command: lpos bigboxlist five | Result: 4
Command: lpos bigboxlist one rank 2 | Result: 5
Command: lpos bigboxlist one rank 5 | Result: null
Command: lpos bigboxlist one count 2 | Result: 0,5
Command: lpos bigboxlist one count 8 | Result: 0,5,10,13
Command: lpos bigboxlist one count 0 | Result: 0,5,10,13
Command: lpos bigboxlist one rank 2 count 2 | Result: 5,10
Command: lpos bigboxlist one rank 2 count 0 | Result: 5,10,13
Command: lpos bigboxlist one rank -1 | Result: 13
Command: lpos bigboxlist one rank -1 count 3 | Result: 13,10,5
Command: lpos bigboxlist two maxlen 1 | Result: null
Command: lpos bigboxlist two maxlen 10 | Result: 1
Command: lpos bigboxlist two count 2 maxlen 10 | Result: 1,7
Command: lpos bigboxlist two count 0 maxlen 10 | Result: 1,7
Command: lpos bigboxlist two count 0 maxlen 15 | Result: 1,7,11
Command: lpos bigboxlist two maxlen 10 rank -1 | Result: 11
Command: lpos bigboxlist two maxlen 10 rank -2 | Result: 7
Command: lpos bigboxlist three maxlen 10 rank -1 | Result: null

Command: lpos bigboxlist nonexistingitem | Result: null

Command: set mystr "my string value here" | Result: OK

Command: lpos mystr m | Error:  [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]

Command: lpos bigboxlist one count -3 | Error:  [ErrorReply: ERR COUNT can't be negative]

Command: lpos bigboxlist one maxlen -3 | Error:  [ErrorReply: ERR MAXLEN can't be negative]

Notes

  • Use the function “lPos” and “lPosCount” from the package node-redis.
  • Here is the signature of “lPos” –
    lPos(key: RedisCommandArgument, element: RedisCommandArgument, options?: LPosOptions): RedisCommandArguments
  • Signature of “lPosCount” –
    lPosCount(key: RedisCommandArgument, element: RedisCommandArgument, count: number, options?: LPosOptions): RedisCommandArguments
// Redis LPOS command example in Java

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.LPosParams;

import java.util.List;

public class Lpos {
    public static void main(String[] args) {
        // Create connection pool
        JedisPool jedisPool = new JedisPool("localhost", 6379);

        try (Jedis jedis = jedisPool.getResource()) {

            /**
             * Push items to list
             *
             * Command: rpush bigboxlist one two three four five one testA two testB testC one two nine one nine
             * Result: (integer) 15
             */
            long listPushResult = jedis.rpush("bigboxlist", "one", "two", "three", "four", "five", "one", "testA", "two", "testB", "testC", "one", "two", "nine", "one", "nine");

            System.out.println("Command: rpush bigboxlist one two three four five one testA two testB testC one two nine one nine | Result:" + listPushResult);

            /**
             * Check list items
             *
             * Command: lrange bigboxlist 0 -1
             * Result:
             *      1) "one"
             *      2) "two"
             *      3) "three"
             *      4) "four"
             *      5) "five"
             *      6) "one"
             *      7) "testA"
             *      8) "two"
             *      9) "testB"
             *      10) "testC"
             *      11) "one"
             *      12) "two"
             *      13) "nine"
             *      14) "one"
             *      15) "nine"
             */
            List<String> lrangeResult = jedis.lrange("bigboxlist", 0, -1);

            System.out.println("Command: lrange bigboxlist 0 -1 | Result:");

            for (String item : lrangeResult) {
                System.out.println(item);
            }

            /**
             * Check first index of "one"
             *
             * Command: lpos bigboxlist one
             * Result: (integer) 0
             */
            Long lposResult = jedis.lpos("bigboxlist", "one");

            System.out.println("Command: lpos bigboxlist one | Result: " + lposResult);

            /**
             * Check first index of "two"
             *
             * Command: lpos bigboxlist two
             * Result: (integer) 1
             */
            lposResult = jedis.lpos("bigboxlist", "two");

            System.out.println("Command: lpos bigboxlist two | Result: " + lposResult);

            /**
             * Check first index of "five"
             *
             * Command: lpos bigboxlist five
             * Result: (integer) 4
             */
            lposResult = jedis.lpos("bigboxlist", "five");

            System.out.println("Command: lpos bigboxlist five | Result: " + lposResult);

            /**
             * Check second occurrence of "one"
             *
             * Command: lpos bigboxlist one rank 2
             * Result: (integer) 5
             */
            LPosParams lposOption = LPosParams.lPosParams().rank(2);
            lposResult = jedis.lpos("bigboxlist", "one", lposOption);

            System.out.println("Command: lpos bigboxlist one rank 2 | Result: " + lposResult);

            /**
             * Check 5th occurrence of "one"
             * We get (nil) as this item occurs less than 5 times
             *
             * Command: lpos bigboxlist one rank 5
             * Result: (nil)
             */
            lposOption = LPosParams.lPosParams().rank(5);
            lposResult = jedis.lpos("bigboxlist", "one", lposOption);

            System.out.println("Command: lpos bigboxlist one rank 5 | Result: " + lposResult);

            /**
             * Get first 2 occurrance of "one"
             *
             * Command: lpos bigboxlist one count 2
             * Result:
             *      1) (integer) 0
             *      2) (integer) 5
             */
            List<Long> lposResults = jedis.lpos("bigboxlist", "one", LPosParams.lPosParams(), 2);

            System.out.println("Command: lpos bigboxlist one count 2 | Result: " + lposResults.toString());

            /**
             * Try to get first 8 occurrences of "one"             *
             * We get only 4 indexes, as "one" is there only 4 times in the list
             *
             * Command: lpos bigboxlist one count 8
             * Result:
             *      1) (integer) 0
             *      2) (integer) 5
             *      3) (integer) 10
             *      4) (integer) 13
             */
            lposResults = jedis.lpos("bigboxlist", "one", LPosParams.lPosParams(), 8);

            System.out.println("Command: lpos bigboxlist one count 8 | Result: " + lposResults.toString());

            /**
             * Pass count as 0 to return all occurrences
             *
             * Command: lpos bigboxlist one count 0
             * Result:
             *      1) (integer) 0
             *      2) (integer) 5
             *      3) (integer) 10
             *      4) (integer) 13
             */
            lposResults = jedis.lpos("bigboxlist", "one", LPosParams.lPosParams(), 0);

            System.out.println("Command: lpos bigboxlist one count 0 | Result: " + lposResults.toString());

            /**
             * Get 2 occurrences of "one" starting from the 2nd occurrance
             *
             * Command: lpos bigboxlist one rank 2 count 2
             * Result:
             *      1) (integer) 5
             *      2) (integer) 10
             */
            lposOption = LPosParams.lPosParams().rank(2);
            lposResults = jedis.lpos("bigboxlist", "one", lposOption, 2);

            System.out.println("Command: lpos bigboxlist one rank 2 count 2 | Result: " + lposResults.toString());

            /**
             * Get all occurrences of "one" starting from the 2nd occurrance
             *
             * Command: lpos bigboxlist one rank 2 count 0
             * Result:
             *      1) (integer) 5
             *      2) (integer) 10
             *      3) (integer) 13
             */
            lposOption = LPosParams.lPosParams().rank(2);
            lposResults = jedis.lpos("bigboxlist", "one", lposOption, 0);

            System.out.println("Command: lpos bigboxlist one rank 2 count 0 | Result: " + lposResults.toString());

            /**
             * Get one occurence from the end of the list
             *
             * Command: lpos bigboxlist one rank -1
             * Result: (integer) 13
             */
            lposOption = LPosParams.lPosParams().rank(-1);
            lposResult = jedis.lpos("bigboxlist", "one", lposOption);

            System.out.println("Command: lpos bigboxlist one rank -1 | Result: " + lposResult);

            /**
             * Get 3 occurences of "one" from the end
             *
             * Command: lpos bigboxlist one rank -1 count 3
             * Result:
             *      1) (integer) 13
             *      2) (integer) 10
             *      3) (integer) 5
             */
            lposOption = LPosParams.lPosParams().rank(-1);
            lposResults = jedis.lpos("bigboxlist", "one", lposOption, 3);

            System.out.println("Command: lpos bigboxlist one rank -1 count 3 | Result: " + lposResults.toString());

            /**
             * Try to get index of "two" withing first 1 item
             * (nil) is returned as "two" is not there is first 1 item
             *
             * Command: lpos bigboxlist two maxlen 1
             * Result: (nil)
             */
            lposOption = LPosParams.lPosParams().maxlen(1);
            lposResult = jedis.lpos("bigboxlist", "two", lposOption);

            System.out.println("Command: lpos bigboxlist two maxlen 1 | Result: " + lposResult);

            /**
             * Get index of "two" withing first 10 list items
             * We get the index 1, as this is the first occurence
             *
             * Command: lpos bigboxlist two maxlen 10
             * Result: (integer) 1
             */
            lposOption = LPosParams.lPosParams().maxlen(10);
            lposResult = jedis.lpos("bigboxlist", "two", lposOption);

            System.out.println("Command: lpos bigboxlist two maxlen 10 | Result: " + lposResult);

            /**
             * Get 2 occurrences of "two" withing first 10 items
             *
             * Command: lpos bigboxlist two count 2 maxlen 10
             * Result:
             *      1) (integer) 1
             *      2) (integer) 7
             */
            lposOption = LPosParams.lPosParams().maxlen(10);
            lposResults = jedis.lpos("bigboxlist", "two", lposOption, 2);

            System.out.println("Command: lpos bigboxlist two count 2 maxlen 10 | Result: " + lposResults.toString());

            /**
             * Get all occurrences of "two" withing first 10 items
             *
             * Command: lpos bigboxlist two count 0 maxlen 10
             * Result:
             *      1) (integer) 1
             *      2) (integer) 7
             */
            lposOption = LPosParams.lPosParams().maxlen(10);
            lposResults = jedis.lpos("bigboxlist", "two", lposOption, 0);

            System.out.println("Command: lpos bigboxlist two count 0 maxlen 10 | Result: " + lposResults.toString());

            /**
             * Get all occurrences of "two" withing first 15 items
             *
             * Command: lpos bigboxlist two count 0 maxlen 15
             * Result:
             *      1) (integer) 1
             *      2) (integer) 7
             *      3) (integer) 11
             */
            lposOption = LPosParams.lPosParams().maxlen(15);
            lposResults = jedis.lpos("bigboxlist", "two", lposOption, 0);

            System.out.println("Command: lpos bigboxlist two count 0 maxlen 15 | Result: " + lposResults.toString());

            /**
             * Get results from the end and consider 10 items from the end
             *
             * Command: lpos bigboxlist two maxlen 10 rank -1
             * Result: (integer) 11
             */
            lposOption = LPosParams.lPosParams().rank(-1).maxlen(10);
            lposResult = jedis.lpos("bigboxlist", "two", lposOption);

            System.out.println("Command: lpos bigboxlist two maxlen 10 rank -1 | Result: " + lposResult);

            /**
             * Get 2nd occurence from the end and consider 10 items from the end
             *
             * Command: lpos bigboxlist two maxlen 10 rank -2
             * Result: (integer) 7
             */
            lposOption = LPosParams.lPosParams().rank(-2).maxlen(10);
            lposResult = jedis.lpos("bigboxlist", "two", lposOption);

            System.out.println("Command: lpos bigboxlist two maxlen 10 rank -2 | Result: " + lposResult);

            /**
             * Get 1st occurence of "three" from the end and consider 10 items from the end
             * Three does not exist in last 10 items, so we get (nil)
             *
             * Command: lpos bigboxlist three maxlen 10 rank -1
             * Result: (nil)
             */
            lposOption = LPosParams.lPosParams().rank(-1).maxlen(10);
            lposResult = jedis.lpos("bigboxlist", "three", lposOption);

            System.out.println("Command: lpos bigboxlist three maxlen 10 rank -1 | Result: " + lposResult);

            /**
             * Try to get a non existing element from a list
             * We get (nil) value
             *
             * Command: lpos bigboxlist nonexistingitem
             * Result: (nil)
             */
            lposResult = jedis.lpos("bigboxlist", "nonexistingitem");

            System.out.println("Command: lpos bigboxlist nonexistingitem | Result: " + lposResult);

            /**
             * Set a string value
             *
             * Command: set mystr "my string value here"
             * Result: OK
             */
            String setResult = jedis.set("mystr", "my string value here");

            System.out.println("Command: set mystr "my string value here" | Result: " + setResult);

            /**
             * Try to use LPOS command on a string
             * We get an error for the wrong type of operation
             *
             * Command: lpos mystr m
             * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
             */
            try {
                lposResult = jedis.lpos("mystr", "m");

                System.out.println("Command: lpos mystr m | Result: " + lposResult);
            } catch(Exception e) {
                System.out.println("Command: lpos mystr m | Error: " + e.getMessage());
            }

            /**
             * Error returned if COUNT is negative
             *
             * Command: lpos bigboxlist one count -3
             * Result: (error) ERR COUNT can't be negative
             */
            try {
                lposResults = jedis.lpos("bigboxlist", "one", LPosParams.lPosParams(), -3);

                System.out.println("Command: lpos bigboxlist one count -3 | Result: " + lposResults);
            } catch(Exception e) {
                System.out.println("Command: lpos bigboxlist one count -3 | Error: " + e.getMessage());
            }

            /**
             * Error returned if MAXLEN is negative
             *
             * Command: lpos bigboxlist one maxlen -3
             * Result: (error) ERR MAXLEN can't be negative
             */
            try {
                lposResult = jedis.lpos("bigboxlist", "one", LPosParams.lPosParams().maxlen(-3));

                System.out.println("Command: lpos bigboxlist one maxlen -3 | Result: " + lposResult);
            } catch(Exception e) {
                System.out.println("Command: lpos bigboxlist one maxlen -3 | Error: " + e.getMessage());
            }

        }

        jedisPool.close();

    }
}

Output:

Command: rpush bigboxlist one two three four five one testA two testB testC one two nine one nine | Result:15

Command: lrange bigboxlist 0 -1 | Result:
one
two
three
four
five
one
testA
two
testB
testC
one
two
nine
one
nine

Command: lpos bigboxlist one | Result: 0
Command: lpos bigboxlist two | Result: 1
Command: lpos bigboxlist five | Result: 4
Command: lpos bigboxlist one rank 2 | Result: 0
Command: lpos bigboxlist one rank 5 | Result: null
Command: lpos bigboxlist one count 2 | Result: [0, 5]
Command: lpos bigboxlist one count 8 | Result: [0, 5, 10, 13]
Command: lpos bigboxlist one count 0 | Result: [0, 5, 10, 13]
Command: lpos bigboxlist one rank 2 count 2 | Result: [5, 10]
Command: lpos bigboxlist one rank 2 count 0 | Result: [5, 10, 13]
Command: lpos bigboxlist one rank -1 | Result: 13
Command: lpos bigboxlist one rank -1 count 3 | Result: [13, 10, 5]
Command: lpos bigboxlist two maxlen 1 | Result: null
Command: lpos bigboxlist two maxlen 10 | Result: 1
Command: lpos bigboxlist two count 2 maxlen 10 | Result: [1, 7]
Command: lpos bigboxlist two count 0 maxlen 10 | Result: [1, 7]
Command: lpos bigboxlist two count 0 maxlen 15 | Result: [1, 7, 11]
Command: lpos bigboxlist two maxlen 10 rank -1 | Result: 11
Command: lpos bigboxlist two maxlen 10 rank -2 | Result: 7
Command: lpos bigboxlist three maxlen 10 rank -1 | Result: null
Command: lpos bigboxlist nonexistingitem | Result: null

Command: set mystr "my string value here" | Result: OK

Command: lpos mystr m | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: lpos bigboxlist one count -3 | Error: ERR COUNT can't be negative
Command: lpos bigboxlist one maxlen -3 | Error: ERR MAXLEN can't be negative

Notes

  • Use method “lpos” from Jedis package.
  • This “lpos” method has 3 variations. Signature of the methods are-
    public Long lpos(final String key, final String element)
    public Long lpos(final String key, final String element, final LPosParams params)
    public List<Long> lpos(final String key, final String element, final LPosParams params, final long count)
// Redis LPOS command examples in C#

using StackExchange.Redis;

namespace Lpos
{
    internal class Program
    {
        static void Main(string[] args)
        {
            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
            IDatabase rdb = redis.GetDatabase();

            /**
             * Push items to list
             *
             * Command: rpush bigboxlist one two three four five one testA two testB testC one two nine one nine
             * Result: (integer) 15
             */
            long listPushResult = rdb.ListRightPush("bigboxlist", new RedisValue[] { "one", "two", "three", "four", "five", "one", "testA", "two", "testB", "testC", "one", "two", "nine", "one", "nine" });

            Console.WriteLine("Command: rpush bigboxlist one two three four five one testA two testB testC one two nine one nine | Result:" + listPushResult);

            /**
             * Check list items
             *
             * Command: lrange bigboxlist 0 -1
             * Result:
             *      1) "one"
             *      2) "two"
             *      3) "three"
             *      4) "four"
             *      5) "five"
             *      6) "one"
             *      7) "testA"
             *      8) "two"
             *      9) "testB"
             *      10) "testC"
             *      11) "one"
             *      12) "two"
             *      13) "nine"
             *      14) "one"
             *      15) "nine"
             */
            RedisValue[] lrangeResult = rdb.ListRange("bigboxlist", 0, -1);
            Console.WriteLine("Command: lrange bigboxlist 0 -1 | Result:" + string.Join(", ", lrangeResult));

            /**
             * Check first index of "one"
             *
             * Command: lpos bigboxlist one
             * Result: (integer) 0
             */
            long lposResult = rdb.ListPosition("bigboxlist", "one");
            Console.WriteLine("Command: lpos bigboxlist one | Result: " + lposResult);

            /**
             * Check first index of "two"
             *
             * Command: lpos bigboxlist two
             * Result: (integer) 1
             */
            lposResult = rdb.ListPosition("bigboxlist", "two");

            Console.WriteLine("Command: lpos bigboxlist two | Result: " + lposResult);

            /**
             * Check first index of "five"
             *
             * Command: lpos bigboxlist five
             * Result: (integer) 4
             */
            lposResult = rdb.ListPosition("bigboxlist", "five");

            Console.WriteLine("Command: lpos bigboxlist five | Result: " + lposResult);

            /**
             * Check second occurrence of "one"
             *
             * Command: lpos bigboxlist one rank 2
             * Result: (integer) 5
             */
            lposResult = rdb.ListPosition("bigboxlist", "one", rank: 2);

            Console.WriteLine("Command: lpos bigboxlist one rank 2 | Result: " + lposResult);

            /**
             * Check 5th occurrence of "one"
             * We get (nil) as this item occurs less than 5 times
             *
             * Command: lpos bigboxlist one rank 5
             * Result: (nil)
             */
            lposResult = rdb.ListPosition("bigboxlist", "one", rank: 5);

            Console.WriteLine("Command: lpos bigboxlist one rank 5 | Result: " + lposResult);

            /**
             * Get first 2 occurrance of "one"
             *
             * Command: lpos bigboxlist one count 2
             * Result:
             *      1) (integer) 0
             *      2) (integer) 5
             */
             long[] lposResults = rdb.ListPositions("bigboxlist", "one", 2);

            Console.WriteLine("Command: lpos bigboxlist one count 2 | Result: " + string.Join(", ", lposResults));

            /**
             * Try to get first 8 occurrences of "one"
             * We get only 4 indexes, as "one" is there only 4 times in the list
             *
             * Command: lpos bigboxlist one count 8
             * Result:
             *      1) (integer) 0
             *      2) (integer) 5
             *      3) (integer) 10
             *      4) (integer) 13
             */
            lposResults = rdb.ListPositions("bigboxlist", "one", 8);

            Console.WriteLine("Command: lpos bigboxlist one count 8 | Result: " + string.Join(", ", lposResults));

            /**
             * Pass count as 0 to return all occurrences
             *
             * Command: lpos bigboxlist one count 0
             * Result:
             *      1) (integer) 0
             *      2) (integer) 5
             *      3) (integer) 10
             *      4) (integer) 13
             */
            lposResults = rdb.ListPositions("bigboxlist", "one", 0);

            Console.WriteLine("Command: lpos bigboxlist one count 0 | Result: " + string.Join(", ", lposResults));

            /**
             * Get 2 occurrences of "one" starting from the 2nd occurrance
             *
             * Command: lpos bigboxlist one rank 2 count 2
             * Result:
             *      1) (integer) 5
             *      2) (integer) 10
             */
            lposResults = rdb.ListPositions("bigboxlist", "one", 2, rank: 2);

            Console.WriteLine("Command: lpos bigboxlist one rank 2 count 2 | Result: " + string.Join(", ", lposResults));

            /**
             * Get all occurrences of "one" starting from the 2nd occurrance
             *
             * Command: lpos bigboxlist one rank 2 count 0
             * Result:
             *      1) (integer) 5
             *      2) (integer) 10
             *      3) (integer) 13
             */
            lposResults = rdb.ListPositions("bigboxlist", "one", 0, rank: 2);

            Console.WriteLine("Command: lpos bigboxlist one rank 2 count 0 | Result: " + string.Join(", ", lposResults));

            /**
             * Get one occurence from the end of the list
             *
             * Command: lpos bigboxlist one rank -1
             * Result: (integer) 13
             */
            lposResult = rdb.ListPosition("bigboxlist", "one", rank: -1);

            Console.WriteLine("Command: lpos bigboxlist one rank -1 | Result: " + lposResult);

            /**
             * Get 3 occurences of "one" from the end
             *
             * Command: lpos bigboxlist one rank -1 count 3
             * Result:
             *      1) (integer) 13
             *      2) (integer) 10
             *      3) (integer) 5
             */
            lposResults = rdb.ListPositions("bigboxlist", "one", 3, rank: -1);

            Console.WriteLine("Command: lpos bigboxlist one rank -1 count 3 | Result: " + string.Join(", ", lposResults));

            /**
             * Try to get index of "two" withing first 1 item
             * (nil) is returned as "two" is not there is first 1 item
             *
             * Command: lpos bigboxlist two maxlen 1
             * Result: (nil)
             */
            lposResult = rdb.ListPosition("bigboxlist", "two", maxLength: 1);

            Console.WriteLine("Command: lpos bigboxlist two maxlen 1 | Result: " + lposResult);

            /**
             * Get index of "two" withing first 10 list items
             * We get the index 1, as this is the first occurence
             *
             * Command: lpos bigboxlist two maxlen 10
             * Result: (integer) 1
             */
            lposResult = rdb.ListPosition("bigboxlist", "two", maxLength: 10);

            Console.WriteLine("Command: lpos bigboxlist two maxlen 10 | Result: " + lposResult);

            /**
             * Get 2 occurrences of "two" withing first 10 items
             *
             * Command: lpos bigboxlist two count 2 maxlen 10
             * Result:
             *      1) (integer) 1
             *      2) (integer) 7
             */
            lposResults = rdb.ListPositions("bigboxlist", "two", 2, maxLength: 10);

            Console.WriteLine("Command: lpos bigboxlist two count 2 maxlen 10 | Result: " + string.Join(", ", lposResults));

            /**
             * Get all occurrences of "two" withing first 10 items
             *
             * Command: lpos bigboxlist two count 0 maxlen 10
             * Result:
             *      1) (integer) 1
             *      2) (integer) 7
             */
            lposResults = rdb.ListPositions("bigboxlist", "two", 0, maxLength: 10);

            Console.WriteLine("Command: lpos bigboxlist two count 0 maxlen 10 | Result: " + string.Join(", ", lposResults));

            /**
             * Get all occurrences of "two" withing first 15 items
             *
             * Command: lpos bigboxlist two count 0 maxlen 15
             * Result:
             *      1) (integer) 1
             *      2) (integer) 7
             *      3) (integer) 11
             */
            lposResults = rdb.ListPositions("bigboxlist", "two", 0, maxLength: 15);

            Console.WriteLine("Command: lpos bigboxlist two count 0 maxlen 15 | Result: " + string.Join(", ", lposResults));

            /**
             * Get results from the end and consider 10 items from the end
             *
             * Command: lpos bigboxlist two maxlen 10 rank -1
             * Result: (integer) 11
             */
            lposResult = rdb.ListPosition("bigboxlist", "two", maxLength: 10, rank: -1);

            Console.WriteLine("Command: lpos bigboxlist two maxlen 10 rank -1 | Result: " + lposResult);

            /**
             * Get 2nd occurence from the end and consider 10 items from the end
             *
             * Command: lpos bigboxlist two maxlen 10 rank -2
             * Result: (integer) 7
             */
            lposResult = rdb.ListPosition("bigboxlist", "two", maxLength: 10, rank: -2);

            Console.WriteLine("Command: lpos bigboxlist two maxlen 10 rank -2 | Result: " + lposResult);

            /**
             * Get 1st occurence of "three" from the end and consider 10 items from the end
             * Three does not exist in last 10 items, so we get (nil)
             *
             * Command: lpos bigboxlist three maxlen 10 rank -1
             * Result: (nil)
             */
            lposResult = rdb.ListPosition("bigboxlist", "three", maxLength: 10, rank: -1);

            Console.WriteLine("Command: lpos bigboxlist three maxlen 10 rank -1 | Result: " + lposResult);

            /**
             * Try to get a non existing element from a list
             * We get (nil) value
             *
             * Command: lpos bigboxlist nonexistingitem
             * Result: (nil)
             */
            lposResult = rdb.ListPosition("bigboxlist", "nonexistingitem");

            Console.WriteLine("Command: lpos bigboxlist nonexistingitem | Result: " + lposResult);

            /**
             * Set a string value
             *
             * Command: set mystr "my string value here"
             * Result: OK
             */
            bool setResult = rdb.StringSet("mystr", "my string value here");

            Console.WriteLine("Command: set mystr "my string value here" | Result: " + setResult);

            /**
             * Try to use LPOS command on a string
             * We get an error for the wrong type of operation
             *
             * Command: lpos mystr m
             * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
             */
            try
            {
                lposResult = rdb.ListPosition("mystr", "m");

                Console.WriteLine("Command: lpos mystr m | Result: " + lposResult);
            }
            catch (Exception e)
            {
                Console.WriteLine("Command: lpos mystr m | Error: " + e.Message);
            }

            /**
             * Error returned if COUNT is negative
             *
             * Command: lpos bigboxlist one count -3
             * Result: (error) ERR COUNT can't be negative
             */
            try
            {
                lposResults = rdb.ListPositions("bigboxlist", "one", -3);

                Console.WriteLine("Command: lpos bigboxlist one count -3 | Result: " + string.Join(", ", lposResults));
            }
            catch (Exception e)
            {
                Console.WriteLine("Command: lpos bigboxlist one count -3 | Error: " + e.Message);
            }

            /**
             * Error returned if MAXLEN is negative
             *
             * Command: lpos bigboxlist one maxlen -3
             * Result: (error) ERR MAXLEN can't be negative
             */
            try
            {
                lposResult = rdb.ListPosition("bigboxlist", "one", maxLength: -3);

                Console.WriteLine("Command: lpos bigboxlist one maxlen -3 | Result: " + lposResult);
            }
            catch (Exception e)
            {
                Console.WriteLine("Command: lpos bigboxlist one maxlen -3 | Error: " + e.Message);
            }

        }
    }
}

Output:

Command: rpush bigboxlist one two three four five one testA two testB testC one two nine one nine | Result:15
Command: lrange bigboxlist 0 -1 | Result:one, two, three, four, five, one, testA, two, testB, testC, one, two, nine, one, nine

Command: lpos bigboxlist one | Result: 0
Command: lpos bigboxlist two | Result: 1
Command: lpos bigboxlist five | Result: 4
Command: lpos bigboxlist one rank 2 | Result: 5
Command: lpos bigboxlist one rank 5 | Result: -1
Command: lpos bigboxlist one count 2 | Result: 0, 5
Command: lpos bigboxlist one count 8 | Result: 0, 5, 10, 13
Command: lpos bigboxlist one count 0 | Result: 0, 5, 10, 13
Command: lpos bigboxlist one rank 2 count 2 | Result: 5, 10
Command: lpos bigboxlist one rank 2 count 0 | Result: 5, 10, 13
Command: lpos bigboxlist one rank -1 | Result: 13
Command: lpos bigboxlist one rank -1 count 3 | Result: 13, 10, 5
Command: lpos bigboxlist two maxlen 1 | Result: -1
Command: lpos bigboxlist two maxlen 10 | Result: 1
Command: lpos bigboxlist two count 2 maxlen 10 | Result: 1, 7
Command: lpos bigboxlist two count 0 maxlen 10 | Result: 1, 7
Command: lpos bigboxlist two count 0 maxlen 15 | Result: 1, 7, 11
Command: lpos bigboxlist two maxlen 10 rank -1 | Result: 11
Command: lpos bigboxlist two maxlen 10 rank -2 | Result: 7
Command: lpos bigboxlist three maxlen 10 rank -1 | Result: -1

Command: lpos bigboxlist nonexistingitem | Result: -1
Command: set mystr "my string value here" | Result: True

Command: lpos mystr m | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: lpos bigboxlist one count -3 | Error: ERR COUNT can't be negative
Command: lpos bigboxlist one maxlen -3 | Error: ERR MAXLEN can't be negative

Notes

  • Use the methods “ListPosition” and “ListPostions” from StackExchange.Redis.
  • Signature of “ListPosition” method is is-
    long ListPosition(RedisKey key, RedisValue element, long rank = 1, long maxLength = 0, CommandFlags flags = CommandFlags.None)
  • Signature of “ListPositions” –
    long[] ListPositions(RedisKey key, RedisValue element, long count, long rank = 1, long maxLength = 0, CommandFlags flags = CommandFlags.None)
# Redis LPOS command example in Python

import redis
import time

# Create Redis client
redisClient = redis.Redis(host='localhost', port=6379,
                          username='default', password='',
                          decode_responses=True)


# Push items to list
# Command: rpush bigboxlist one two three four five one testA two testB testC one two nine one nine
# Result: (integer) 15
commandResult = redisClient.rpush("bigboxlist",
  "one",
  "two",
  "three",
  "four",
  "five",
  "one",
  "testA",
  "two",
  "testB",
  "testC",
  "one",
  "two",
  "nine",
  "one",
  "nine",
)

print("Command: rpush bigboxlist one two three four five one testA two testB testC one two nine one nine | Result: {}".format(commandResult))

# Check list items
# Command: lrange bigboxlist 0 -1
# Result:
#      1) "one"
#      2) "two"
#      3) "three"
#      4) "four"
#      5) "five"
#      6) "one"
#      7) "testA"
#      8) "two"
#      9) "testB"
#      10) "testC"
#      11) "one"
#      12) "two"
#      13) "nine"
#      14) "one"
#      15) "nine"
commandResult = redisClient.lrange("bigboxlist", 0, -1)

print("Command: lrange bigboxlist 0 -1 | Result: {}".format(commandResult))

# Check first index of "one"
# Command: lpos bigboxlist one
# Result: (integer) 0
commandResult = redisClient.lpos("bigboxlist", "one")

print("Command: lpos bigboxlist one | Result: {}".format(commandResult))

# Check first index of "two"
# Command: lpos bigboxlist two
# Result: (integer) 1
commandResult = redisClient.lpos("bigboxlist", "two")

print("Command: lpos bigboxlist two | Result: {}".format(commandResult))

# Check first index of "five"
# Command: lpos bigboxlist five
# Result: (integer) 4
commandResult = redisClient.lpos("bigboxlist", "five")

print("Command: lpos bigboxlist five | Result: {}".format(commandResult))

# Check second occurrence of "one"
# Command: lpos bigboxlist one rank 2
# Result: (integer) 5
commandResult = redisClient.lpos("bigboxlist", "one", rank= 2)

print("Command: lpos bigboxlist one rank 2 | Result: {}".format(commandResult))

# Check 5th occurrence of "one"
# We get (nil) as this item occurs less than 5 times
# Command: lpos bigboxlist one rank 5
# Result: (nil)
commandResult = redisClient.lpos("bigboxlist", "one", rank= 5)

print("Command: lpos bigboxlist one rank 5 | Result: {}".format(commandResult))

# Get first 2 occurrance of "one"
# Command: lpos bigboxlist one count 2
# Result:
#      1) (integer) 0
#      2) (integer) 5
commandResult = redisClient.lpos("bigboxlist", "one", count=2)

print("Command: lpos bigboxlist one count 2 | Result: {}".format(commandResult))

# Try to get first 8 occurrences of "one"            #
# We get only 4 indexes, as "one" is there only 4 times in the list
# Command: lpos bigboxlist one count 8
# Result:
#      1) (integer) 0
#      2) (integer) 5
#      3) (integer) 10
#      4) (integer) 13
commandResult = redisClient.lpos("bigboxlist", "one", count= 8)

print("Command: lpos bigboxlist one count 8 | Result: {}".format(commandResult))

# Pass count as 0 to return all occurrences
# Command: lpos bigboxlist one count 0
# Result:
#      1) (integer) 0
#      2) (integer) 5
#      3) (integer) 10
#      4) (integer) 13
commandResult = redisClient.lpos("bigboxlist", "one", count= 0)

print("Command: lpos bigboxlist one count 0 | Result: {}".format(commandResult))

# Get 2 occurrences of "one" starting from the 2nd occurrance
# Command: lpos bigboxlist one rank 2 count 2
# Result:
#      1) (integer) 5
#      2) (integer) 10
commandResult = redisClient.lpos("bigboxlist", "one", count= 2, rank= 2)

print("Command: lpos bigboxlist one rank 2 count 2 | Result: {}".format(commandResult))

# Get all occurrences of "one" starting from the 2nd occurrance
# Command: lpos bigboxlist one rank 2 count 0
# Result:
#      1) (integer) 5
#      2) (integer) 10
#      3) (integer) 13
commandResult = redisClient.lpos("bigboxlist", "one", count= 0, rank= 2)

print("Command: lpos bigboxlist one rank 2 count 0 | Result: {}".format(commandResult))

# Get one occurence from the end of the list
# Command: lpos bigboxlist one rank -1
# Result: (integer) 13
commandResult = redisClient.lpos("bigboxlist", "one", rank= -1)

print("Command: lpos bigboxlist one rank -1 | Result: {}".format(commandResult))

# Get 3 occurences of "one" from the end
# Command: lpos bigboxlist one rank -1 count 3
# Result:
#      1) (integer) 13
#      2) (integer) 10
#      3) (integer) 5
commandResult = redisClient.lpos("bigboxlist", "one", count= 3, rank= -1)

print("Command: lpos bigboxlist one rank -1 count 3 | Result: {}".format(commandResult))

# Try to get index of "two" withing first 1 item
# (nil) is returned as "two" is not there is first 1 item
# Command: lpos bigboxlist two maxlen 1
# Result: (nil)
commandResult = redisClient.lpos("bigboxlist", "two", maxlen= 1)

print("Command: lpos bigboxlist two maxlen 1 | Result: {}".format(commandResult))

# Get index of "two" withing first 10 list items
# We get the index 1, as this is the first occurence
# Command: lpos bigboxlist two maxlen 10
# Result: (integer) 1
commandResult = redisClient.lpos("bigboxlist", "two", maxlen= 10)

print("Command: lpos bigboxlist two maxlen 10 | Result: {}".format(commandResult))

# Get 2 occurrences of "two" withing first 10 items
# Command: lpos bigboxlist two count 2 maxlen 10
# Result:
#      1) (integer) 1
#      2) (integer) 7
commandResult = redisClient.lpos("bigboxlist", "two", count= 2, maxlen= 10)

print("Command: lpos bigboxlist two count 2 maxlen 10 | Result: {}".format(commandResult))

# Get all occurrences of "two" withing first 10 items
# Command: lpos bigboxlist two count 0 maxlen 10
# Result:
#      1) (integer) 1
#      2) (integer) 7
commandResult = redisClient.lpos("bigboxlist", "two", count= 0, maxlen= 10)

print("Command: lpos bigboxlist two count 0 maxlen 10 | Result: {}".format(commandResult))

# Get all occurrences of "two" withing first 15 items
# Command: lpos bigboxlist two count 0 maxlen 15
# Result:
#      1) (integer) 1
#      2) (integer) 7
#      3) (integer) 11
commandResult = redisClient.lpos("bigboxlist", "two", count= 0, maxlen= 15)

print("Command: lpos bigboxlist two count 0 maxlen 15 | Result: {}".format(commandResult))

# Get results from the end and consider 10 items from the end
# Command: lpos bigboxlist two maxlen 10 rank -1
# Result: (integer) 11
commandResult = redisClient.lpos("bigboxlist", "two", maxlen= 10, rank= -1)

print("Command: lpos bigboxlist two maxlen 10 rank -1 | Result: {}".format(commandResult))

# Get 2nd occurence from the end and consider 10 items from the end
# Command: lpos bigboxlist two maxlen 10 rank -2
# Result: (integer) 7
commandResult = redisClient.lpos("bigboxlist", "two", maxlen= 10, rank= -2)

print("Command: lpos bigboxlist two maxlen 10 rank -2 | Result: {}".format(commandResult))

# Get 1st occurence of "three" from the end and consider 10 items from the end
# Three does not exist in last 10 items, so we get (nil)
# Command: lpos bigboxlist three maxlen 10 rank -1
# Result: (nil)
commandResult = redisClient.lpos("bigboxlist", "three", maxlen= 10, rank= -1)

print("Command: lpos bigboxlist three maxlen 10 rank -1 | Result: {}".format(commandResult))

# Try to get a non existing element from a list
# We get (nil) value
# Command: lpos bigboxlist nonexistingitem
# Result: (nil)
commandResult = redisClient.lpos("bigboxlist", "nonexistingitem")

print("Command: lpos bigboxlist nonexistingitem | Result: {}".format(commandResult))

# Set a string value
# Command: set mystr "my string value here"
# Result: OK
commandResult = redisClient.set("mystr", "my string value here")

print("Command: set mystr "my string value here" | Result: {}".format(commandResult))

# Try to use LPOS command on a string
# We get an error for the wrong type of operation
# Command: lpos mystr m
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
try:
  commandResult = redisClient.lpos("mystr", "m")

  print("Command: lpos mystr m | Result: {}".format(commandResult))
except Exception as error:
  print("Command: lpos mystr m | Error: ", error)


# Error returned if COUNT is negative
# Command: lpos bigboxlist one count -3
# Result: (error) ERR COUNT can't be negative
try:
  commandResult = redisClient.lpos("bigboxlist", "one", count= -3)

  print("Command: lpos bigboxlist one count -3 | Result: {}".format(commandResult))
except Exception as error:
  print("Command: lpos bigboxlist one count -3 | Error: ", error)

# Error returned if MAXLEN is negative
# Command: lpos bigboxlist one maxlen -3
# Result: (error) ERR MAXLEN can't be negative
try:
  commandResult = redisClient.lpos("bigboxlist", "one", maxlen= -3)

  print("Command: lpos bigboxlist one maxlen -3 | Result: {}".format(commandResult))
except Exception as error:
  print("Command: lpos bigboxlist one maxlen -3 | Error: ", error)

Output:

Command: rpush bigboxlist one two three four five one testA two testB testC one two nine one nine | Result: 15
Command: lrange bigboxlist 0 -1 | Result: ['one', 'two', 'three', 'four', 'five', 'one', 'testA', 'two', 'testB', 'testC', 'one', 'two', 'nine', 'one', 'nine']

Command: lpos bigboxlist one | Result: 0
Command: lpos bigboxlist two | Result: 1
Command: lpos bigboxlist five | Result: 4
Command: lpos bigboxlist one rank 2 | Result: 5
Command: lpos bigboxlist one rank 5 | Result: None
Command: lpos bigboxlist one count 2 | Result: [0, 5]
Command: lpos bigboxlist one count 8 | Result: [0, 5, 10, 13]
Command: lpos bigboxlist one count 0 | Result: [0, 5, 10, 13]
Command: lpos bigboxlist one rank 2 count 2 | Result: [5, 10]
Command: lpos bigboxlist one rank 2 count 0 | Result: [5, 10, 13]
Command: lpos bigboxlist one rank -1 | Result: 13
Command: lpos bigboxlist one rank -1 count 3 | Result: [13, 10, 5]
Command: lpos bigboxlist two maxlen 1 | Result: None
Command: lpos bigboxlist two maxlen 10 | Result: 1
Command: lpos bigboxlist two count 2 maxlen 10 | Result: [1, 7]
Command: lpos bigboxlist two count 0 maxlen 10 | Result: [1, 7]
Command: lpos bigboxlist two count 0 maxlen 15 | Result: [1, 7, 11]
Command: lpos bigboxlist two maxlen 10 rank -1 | Result: 11
Command: lpos bigboxlist two maxlen 10 rank -2 | Result: 7
Command: lpos bigboxlist three maxlen 10 rank -1 | Result: None

Command: lpos bigboxlist nonexistingitem | Result: None

Command: set mystr "my string value here" | Result: True

Command: lpos mystr m | Error:  WRONGTYPE Operation against a key holding the wrong kind of value
Command: lpos bigboxlist one count -3 | Error:  COUNT can't be negative
Command: lpos bigboxlist one maxlen -3 | Error:  MAXLEN can't be negative

Notes

  • Use method “lpos” from redis-py.
  • Signature of the method is –
    def lpos(self, name: str, value: str, rank: Optional[int] = None, count: Optional[int] = None, maxlen: Optional[int] = None) -> Union[str, List, None]

Output:

Notes

  • Use the method “llen” of predis.
  • Signature of the method is-

Output:

Notes

  • Use method “linsert” from the redis-rb.
  • Signature of the method is-

    # @param [String] key
    # @param [String, Symbol] where `BEFORE` or `AFTER`
    # @param [String] pivot reference element
    # @param [String] value
    # @return [Integer] length of the list after the insert operation, or `-1`
    #   when the element `pivot` was not found

    def linsert(key, where, pivot, value)

Source Code

Use the following links to get the source code used in this article-

Related Commands

CommandDetails
LPUSH Command Details
RPUSH Command Details
LRANGE Command Details

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.