Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating a redis client hash

I would like to update and add an item to the redis hash session entry.

I have been able to create a hash using the redis client using the code below:

var redis = require('redis');
var client = redis.createClient(); //creates a new client

client.on('connect', function() {
    console.log('connected');
});

client.hmset('frameworks', {
    'javascript': 'AngularJS',
    'css': 'Bootstrap',
    'node': 'Express'
});

Is there a way of adding to this hash? I would like to change and also to update an existing hash element.

Is this possible without reading everything and creating a new hash with updated and new hash elements.

I am using this webpage as a tutorial guide : https://www.sitepoint.com/using-redis-node-js/

like image 713
Ethan Richardson Avatar asked Dec 05 '22 00:12

Ethan Richardson


1 Answers

You can use same hmset/hset based on how many you want to add or update

var redis = require('redis');
var client = redis.createClient(); //creates a new client

client.on('connect', function() {
    console.log('connected');
});

client.hmset('frameworks', {
    'javascript': 'AngularJS',
    'css': 'Bootstrap',
    'node': 'Express'
});

Say you initially have this and want to add db : mongo, and want to update node: Express4 then you can just use

//If you know will update only one use hset instead
client.hmset('frameworks', {
    'node': 'Express4',
    'db' : 'MongoDB'
});

Will add db & update node too for the key frameworks

like image 137
jerry Avatar answered Jan 07 '23 10:01

jerry