Reputation: 634
How can I store the following structure in redis and get a specific output:
projects is the main category.
projects has categories each category has a list of data.
projects:catega contains: id: 1 values {id: 123, name: 'bla'}
id: 2 values {id: 124, name: 'bla1'}
projects:categb contains: id: 1 values {id: 125, name: 'bla2'}
id: 2 values {id: 126, name: 'bla4'}
I want to be able to get all the by selecting projects all the categories and their data like:
{catega: [{id...}, {id...}], categb: [{id...}, {id...}]}
Upvotes: 0
Views: 5025
Reputation: 1052
Redis can store hash keys not complex objects, however, you can workaround that using below
1- restructure your object to be simpler such as
instead of
project1:
{
catega:
[{id: 123, name: 'bla'}, {id: 124, name: 'bla1'}],
[{id: 123, name: 'bla'}, {id: 124, name: 'bla1'}]
categb:
[{id: 123, name: 'bla'}, {id: 124, name: 'bla1'}],
[{id: 123, name: 'bla'}, {id: 124, name: 'bla1'}]
}
To be
project1:
{
categaID123: '{name:bla, age:15, salary:20}',
categaID124: '{name:blo, age:12, salary:21}',
categbID123: '{name:bla, age:15, salary:20}',
categbID124: '{name:bla, age:15, salary:20}',
}
2- You can set the hash as follows
hset project2 categorybID123 "{name:bla, age:15, salary:20}"
Or you can set all keys in one shot but using HMSET
3- You can retrieve the data in one call
hget project2 categorybID123
Hope this helps
Upvotes: 2