linsert.3valkey - Man Page
Inserts an element before or after another element in a list.
Synopsis
LINSERT key <BEFORE | AFTER> pivot element
Description
Inserts element in the list stored at key either before or after the reference value pivot.
When key does not exist, it is considered an empty list and no operation is performed.
An error is returned when key exists but does not hold a list value.
Reply
One of the following:
- valkey-protocol(7) Integer reply: the list length after a successful insert operation.
- valkey-protocol(7) Integer reply:
0when the key doesn’t exist. - valkey-protocol(7) Integer reply:
-1when the pivot wasn’t found.
Complexity
O(N) where N is the number of elements to traverse before seeing the value pivot. This means that inserting somewhere on the left end on the list (head) can be considered O(1) and inserting somewhere on the right end (tail) is O(N).
Acl Categories
@list @slow @write
History
- Available since: 2.2.0
Examples
127.0.0.1:6379> RPUSH mylist "Hello" (integer) 1 127.0.0.1:6379> RPUSH mylist "World" (integer) 2 127.0.0.1:6379> LINSERT mylist BEFORE "World" "There" (integer) 3 127.0.0.1:6379> LRANGE mylist 0 -1 1) "Hello" 2) "There" 3) "World"
See Also
blmove(3valkey), blmpop(3valkey), blpop(3valkey), brpop(3valkey), lindex(3valkey), llen(3valkey), lmove(3valkey), lmpop(3valkey), lpop(3valkey), lpos(3valkey), lpush(3valkey), lpushx(3valkey), lrange(3valkey), lrem(3valkey), lset(3valkey), ltrim(3valkey), rpop(3valkey), rpush(3valkey), rpushx(3valkey)