Reputation: 63
I'm very new to Redis and might have missed something obvious, so excuse me if I'm missing something easy.
In a hash in Redis, I have my set of attributes I want to set with HMSET, but one of my attributes needs to have sub-attributes in it. It's meant to be a Seller attribute with sub-attributes "name", "location", and "phone number." How does this work? One of the hashes I'm trying to set looks like:
hmset ad:1 car:"2018 Chevy Colorado" Year:"2018" Miles:"4" Seller:(need sub-attributes)
Thank you very much!
Upvotes: 1
Views: 307
Reputation: 31528
Redis does not support sub-attributes in a hash, or in any of it's data structures. So you have to build your own convention to store sub-attributes.
People generally do one of the following -
You can move the seller into it's own hash, and then store only the seller id in your car object. Retrieve both the objects in your application and merge it. This approach also scales nicely if you have more than 1 seller. Just store all the ids in a set or a list.
Or you can "flatten" the object. For example, instead of 1 field "seller", store 3 fields "seller.name", "seller.phone", "seller.location". Some redis libraries (see spring in java) can automatically do this conversion for you.
Instead of using a hash, serialize your object and store it in a redis string. There are various ways to serialize a complex object. You can use JSON, messagepack, protocol buffers etc. Or you can use your programming language's default serialization mechanism.
Upvotes: 1