3

I was trying to manage a list in redis with nodejs. I am able to store list with following code :

client.rpush(['room_'+room, data.customClient], function(err, reply) {

        client.lrange('room_'+room, 0, -1, function(err, reply) {
          console.log(reply);
      });
    });

The console output is

[ 'POanqn9llyTcuIurUPAN', 'm9vd57wecp6JvtHOrRSJ' ]

Now I want to delete one key POanqn9llyTcuIurUPAN from this list. How can I do this ?

6
  • Your Redis client should have an interface to the LREM command: redis.io/commands/lrem Commented Jun 15, 2016 at 16:21
  • Yes, But in Node.js library how can I call LREM ?? Commented Jun 15, 2016 at 16:32
  • Perhaps call client.lrem('room_'+room,reply,1); where you log the reply from lrange Commented Jun 15, 2016 at 16:54
  • Note, however, that lpop is probably what you're looking for. Commented Jun 15, 2016 at 16:56
  • @ItamarHaber can you please tell the syntax for lpop, I didn't find it in documentation. Commented Jun 15, 2016 at 17:09

2 Answers 2

1

It can be done through lrem command. The usage to delete all the entries with that value will be:

client.lrem('room_'+room, data.customClient, 0, 'POanqn9llyTcuIurUPAN', function(err, data){
    console.log(data); // Tells how many entries got deleted from the list
});

Here 0 is a count telling delete all the entries with the value POanqn9llyTcuIurUPAN in the given list. From https://redis.io/commands/lrem the possible values of the count could be:

  • count > 0: Remove elements equal to value moving from head to tail.
  • count < 0: Remove elements equal to value moving from tail to head.
  • count = 0: Remove all elements equal to value.
Sign up to request clarification or add additional context in comments.

2 Comments

Why is data.customClient, passed into the lrem?
so wierd, you can access by index but not remove by index. why. not o(1). requires search for value. further you can not say to a list to remove a value starting from a certain index. its either head or tail meaning you can not be sure to be removing the same index.
0

it can be done easily like this:

client.lrem('room_'+room, 0, 'POanqn9llyTcuIurUPAN', function(err, data){
    console.log(data); // Tells how many entries got deleted from the list
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.