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

2 months ago Lalit Bhagtani 0

In this tutorial, we will learn about how to insert a new element around a pivot element in the list value stored at key. For this, we will use Redis LINSERT command.

LINSERT Command

This command insert a new element in the list value stored at specified key, either before or after a specific element (Pivot). It returns 0, when key does not exist as it is interpreted as an empty list and returns -1, 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 :-

Redis LINSERT

References :-

  1. LINSERT Command Docs

That’s all for how to insert an element around a pivot 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.

<- List Commands