LINSERT
LINSERT key BEFORE | AFTER pivot element
- Group
- List
- Complexity
- O(N) where N is the number of elements before the pivot
- Arity
- 5 (exactly, counting the command name)
- Flags
- write
- Keys
- first 1, last 1, step 1
- HotKV Cloud
- All plans
- On-Premise
- no license needed
LINSERT finds the first occurrence of pivot in the list stored at key,
scanning from the head, and inserts element just before or just after it.
If pivot is not in the list, nothing changes. A missing key is treated as an
empty list and is not created.
Options
Section titled “Options”| Option | Effect |
|---|---|
BEFORE |
Insert element immediately before pivot. |
AFTER |
Insert element immediately after pivot. |
Return value
Section titled “Return value”| Case | RESP2 | RESP3 |
|---|---|---|
| Inserted | Integer: the length of the list after the insert | Same |
pivot not found |
Integer -1 |
Same |
| Key does not exist | Integer 0 |
Same |
Errors:
ERR syntax errorif the second argument is notBEFOREorAFTER.WRONGTYPEifkeyholds a value that is not a list.
Examples
Section titled “Examples”127.0.0.1:6379> RPUSH steps "one" "three"(integer) 2127.0.0.1:6379> LINSERT steps BEFORE "three" "two"(integer) 3127.0.0.1:6379> LINSERT steps AFTER "nine" "ten"(integer) -1127.0.0.1:6379> LRANGE steps 0 -11) "one"2) "two"3) "three"Compatibility
Section titled “Compatibility”Same as Redis 7.4 and Valkey 8.
See also
Section titled “See also”Was this page helpful?
Thanks for the feedback.