Redis LINSERT – How to insert element in list before | after pivot element

4 days ago Lalit Bhagtani 0

In this tutorial, we will learn about how to insert a new element in the list value stored at key, either before or after a specific element (Pivot). For this we will use a COMMAND – LINSERT in redis-cli. 0 is returned, when key does not exist as it is interpreted as an empty list and -1 is returned, when key exist but pivot element does not exist in the list. The syntax of redis LINSERT command is as follows :-

Syntax :-

redis host:post> LINSERT <key name> [BEFORE|AFTER] <pivot element> <new element>

Output :- 

- (integer) value, representing the number of elements in the list.
- -1, if key exist but pivot element is not present in the list.
- 0, if key does not exist.
- Error, if key exist and value stored at the key is not a list.

Example :-


References :-

  1. LINSERT Command Docs

That’s all for how to insert an element in the list value stored in redis datastore, either before or after the pivot element. If you liked it, please share your thoughts in comments section and share it with others too.