我有一个json格式的用户列表,我想将其显示为用户界面上的管理用户表。我想存储我所编辑的所有用户,我想这就是我要存储的
JSON.SET user $ '{"id":"1","user_id":"1","one": "1","two": "2","three": "3","rank_score": 1.44444}' EX 60
JSON.SET user $ '{"id":"2","user_id":"2","one": "2","two": "2","three": "3","rank_score": 2.44444}'
JSON.SET user $ '{"id":"3","user_id":"3","one": "3","two": "3","three": "3","rank_score": 3.44444}'
JSON.SET user $ '{"id":"4","user_id":"4","one": "4","two": "4","three": "4","rank_score": 4.44444}'然后得到所有的记录,user
此命令JSON.GET user $只返回第4条记录。
多个命令还是给了我一张唱片
127.0.0.1:6379> multi
OK
127.0.0.1:6379(TX)> json.get user
QUEUED
127.0.0.1:6379(TX)> exec
1) "{\"id\":\"4\",\"user_id\":\"4\",\"one\":\"4\",\"two\":\"4\",\"three\":\"4\",\"rank_score\":4.44444}"我想:
> Get all
> Get all records where id is a given id
> Get all records order by rank_score desc
> Remove record where id is 2如何对所有用户进行分组,以便能够同时获得所有用户?
编辑:
我让users_list把所有的东西都嵌套在
JSON.SET user $ '{"users_list":[{"id":"1","user_id":"1","one": "1","two": "1","three": "1","rank_score": 1.44444},{"id":"2","user_id":"2","one": "2","two": "2","three": "2","rank_score": 2.44444},{"id":"3","user_id":"3","one": "3","two": "3","three": "3","rank_score": 3.44444},{"id":"4","user_id":"4","one": "4","two": "4","three": "4","rank_score": 4.44444}]}'可读性
JSON.SET user $ '{
"users_list":[
{
"id":"1",
"user_id":"1",
"one": "1",
"two": "1",
"three": "1",
"rank_score": 1.44444
},
{
"id":"2",
"user_id":"2",
"one": "2",
"two": "2",
"three": "2",
"rank_score": 2.44444
},
{
"id":"3",
"user_id":"3",
"one": "3",
"two": "3",
"three": "3",
"rank_score": 3.44444
},
{
"id":"4",
"user_id":"4",
"one": "4",
"two": "4",
"three": "4",
"rank_score": 4.44444
}
]
}'会有超过128个用户导致错误吗?
发布于 2022-11-10 11:09:17
RedisJSON仍然是一个核心的键值存储。只有一个值具有给定的键。当您再次调用具有现有密钥上相同路径的JSON.SET时,所提供的任何值都会覆盖以前存在的值。因此,调用JSON.SET user $ ...是在覆盖键。
与使用user密钥不同,使用user作为前缀、存储名为user:1、user:2等的密钥是一种标准做法。这是可取的,原因有几个(比存储单个JSON文档数组更具有性能,可以为特定的密钥模式定义ACL规则等),但最重要的是针对您的问题:
您可以使用RediSearch的全文搜索,使用FT.SEARCH命令获取符合任何需要的参数组合的键。例如,所有具有特定id的用户。它还允许排序。例如,如果您存储的用户喜欢
JSON.SET user:1 $ '{"id":"1","user_id":"1","one": "1","two": "2","three": "3","rank_score": 1.44444}' EX 60
JSON.SET user:2 $ '{"id":"2","user_id":"2","one": "2","two": "2","three": "3","rank_score": 2.44444}'您可以使用FT.CREATE为任何需要搜索的参数创建索引,例如,要索引它们的id和rank_score,命令是FT.CREATE userSearch ON JSON PREFIX 1 user: SCHEMA id NUMERIC rank_score NUMERIC SORTABLE。为了详细解释,这说明:为JSON类型的键创建一个名为userSearch的搜索索引,用于所有带有一个前缀user:的键,其中包含以下字段:id是一个数字,rank_score是一个可排序的数字。
以下是你提到的事情:
keys user:*
id是一个给定的id (让我们来查找id 9000):FT.SEARCH userSearch "@id:9000"
rank_score desc:FT.SEARCH userSearch "@rank_score:*" SORTBY rank_score DESC
FT.SEARCH userSearch "@id:2",然后针对每个结果JSON.DEL key.要获得更多信息,请阅读正式文档,它将详细描述系统的功能。
https://stackoverflow.com/questions/74359547
复制相似问题