How to store array in Redis hashes?

后端 未结 1 1125
执念已碎
执念已碎 2021-02-01 03:47

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\": \"         


        
相关标签:
1条回答
  • 2021-02-01 04:04

    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

    0 讨论(0)
提交回复
热议问题