I'm very new to Redis, and looking to see if its possible to do. Imagine I'm receiving data like this:
{ "account": "abc", "name": "Bob", "lname": "Smith" }
{ "account": "abc", "name": "Sam", "lname": "Wilson" }
{ "account": "abc", "name": "Joe"}
And receiving this data for another account:
{ "account": "xyz", "name": "Bob", "lname": "Smith" }
{ "account": "xyz", "name": "Sam", "lname": "Smith"}
I would like to keep this data in Redis in similar format:
abc:name ["Bob", "Sam", "Joe"]
abc:lname ["Smith", "Wilson", Null]
And for xyz:
xyz:name["Bob", "Sam"]
xyz:lname["Smith", "Smith"]
So the question is what data types should I use to store this Redis?
If your goal is to check if Bob
is used as a name
for the account abc
the solution should be something like:
Sample Data
{ "account": "abc", "name": "Bob", "lname": "Smith" }
{ "account": "abc", "name": "Sam", "lname": "Wilson" }
{ "account": "abc", "name": "Joe"}
Do this (using a redis set):
SADD abc:name Bob Sam Joe
SADD abc:lname Wilson Smith
You'll then be able to check if Bob
is used as a name
for the account abc
, with:
SISMEMBER abc:name Bob
> true
To retrieve all values of a field use SMEMBERS:
SMEMBERS abc:name
> ["Bob", "Sam", "Joe"]
Note:
- The key name here is under the
[account]:[field]
format. Where [account]
can be abc
, xyz
and so on and field
can be name
, lname
...
If you don't want unique value, for instance:
abc:name ["Bob", "Sam", "Joe", "Bob", "Joe"]
then you should use a list instead