How it works...

Similar to what we mentioned in the Using list data type section, we don't have to initialize an empty hash before adding fields. Redis will do this automatically with HSET and HMSET. Similarly, Redis will do the clean-up work when a hash is empty.

By default, HSET and HMSET will overwrite any existing fields. The HSETNX command, which sets the value of a field only if the field does not exist, can be used to prevent the default overwriting behavior of HSET:> HSETNX "Kyoto Ramen" "phone" "555-555-0001" 
(integer) 0> HGET "Kyoto Ramen" "phone" 

HMGET and HGET will return (nil) for any non-existent key or field:> HMGET "Kyoto Ramen" "rating" "hours" 1) "4.9" 2) (nil) ...

Get Redis 4.x Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.