Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can store a Json in redis with hashmap( HSET )

i have a question about HSET in redis. As far as i know, redis is a key-value database. that means every thing store as a key-value and we don't have table for example. i wanted to save something in redis so i decided to use Hashmap . since the HMSET is deprecated and we should use HSET instead, how should I store many attribute as a value and a id as a key in hset? you know i want to save some thing like this:

await redis.hset(`origin-${originId}`, 'title',title)

but if i have many fields to save i should write this line for each field?? for example :

await redis.hset(`origin-${originId}`, 'title',title)
await redis.hset(`origin-${originId}`, 'status',status)
...

as in HSET we should define 3 parameter i write this code. Isn't there a better solution?

like image 676
Fateme Ahmadi Avatar asked Oct 28 '25 05:10

Fateme Ahmadi


1 Answers

you can do this

hset key field1 value1 field2 value2 ...

It's documented here

https://redis.io/commands/hmset and https://redis.io/commands/hset

but hmset is deprecated and you should use hset now because it supports the same args as hmset

in node.js, if you already have the key / value in an object, you can do this

await redis.hset('myhashset', { field1: value1, field2: value2 })
like image 99
Tuan Anh Tran Avatar answered Oct 29 '25 21:10

Tuan Anh Tran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!