简介

RedisJSON 是一个 Redis 模块,允许从 Redis 键(文档)存储、更新和获取 JSON 值。

主要特点:

  • 完全支持 JSON 标准
  • 用于在文档中选择元素的 类似 JSONPath 的语法
  • 文档以树状结构存储为二进制数据,允许快速访问子元素
  • 所有 JSON 值类型的类型化原子操作

命令格式

JSON.{cmd}  key  JsonPath    value 

127.0.0.1:6380> JSON.SET test . '{"key": "value"}'
OK
127.0.0.1:6380> JSON.SET test ".n" "999"   // 添加数字元素到test
OK
127.0.0.1:6380> JSON.SET test .array '[1, 2, 3]'  // 添加列表元素到test
OK

127.0.0.1:6380> JSON.GET test
"{\"key\":\"value\",\"n\":999,\"array\":[1,2,3]}"
127.0.0.1:6380> JSON.GET test .array[0]     // 按照JSONPath获取对应位置的值
"[1]"

127.0.0.1:6380> JSON.SET test .array[0] 99     //  修改array 列表中第一个元素值为99
OK
127.0.0.1:6380> JSON.GET test
"{\"key\":\"value\",\"n\":999,\"array\":[99,2,3]}"

对于新的 Redis 键, path 必须是根 即 . 。修改JSON数据使用path指定, 对应数据的位置. 

数据介绍

标准的JSON数据对象, 包括空 字符串, 数字, 布尔,  列表和字典.

1. 数字

标准的redis str数据,保存的是数字的时候,支持增加incr和减少decr方法

127.0.0.1:6380> set num 1
OK
127.0.0.1:6380> incr num
(integer) 2
127.0.0.1:6380> incrby num 2
(integer) 4
127.0.0.1:6380> decr num
(integer) 3
127.0.0.1:6380> INCRBYFLOAT num 1.1
"4.1"

JSON数据有对应数字的操作JSON.NUMINCRBY,  另外还增加了乘法JSON.NUMMULTBY和乘方JSON.NUMPOWBY的操作

127.0.0.1:6380> JSON.NUMINCRBY test .n 1    // 路径n对应数据 +1
"1000"
127.0.0.1:6380> JSON.NUMINCRBY test .n -990  // 路径n对应数据 -990
"10"


127.0.0.1:6380> JSON.NUMMULTBY test .n 2    // 路径n对应数据 *2
"20"
127.0.0.1:6380> JSON.NUMPOWBY test .n 2     // 路径n对应数据 ^2
"400"

127.0.0.1:6380> JSON.GET test
"{\"key\":\"value\",\"n\":400,\"array\":[99,2,3]}"

 2.字符

对应str类型的数据, JSON支持 JSON.STRAPPENDJSON.STRLEN 与对应原生redis的append和strlen命令

127.0.0.1:6380> set a "a"
OK
127.0.0.1:6380> append a "bc"
(integer) 3
127.0.0.1:6380> strlen a
(integer) 3
127.0.0.1:6380> JSON.STRAPPEND test .key '"alo"'
(integer) 8
127.0.0.1:6380> JSON.get test
"{\"key\":\"valuealo\",\"n\":400,\"array\":[99,2,3]}"
127.0.0.1:6380> JSON.STRLEN test .key
(integer) 8

3.数组

使用JSON.ARRAPPEND和JSON.ARRINSERT添加元素

127.0.0.1:6380> JSON.ARRAPPEND test .array 4    // 列表后面添加数据
(integer) 4
127.0.0.1:6380> JSON.GET test .array
"[99,2,3,4]"

127.0.0.1:6380> JSON.ARRINSERT test .array 0 '"new"'    // 插入到对应坐标位置
(integer) 5
127.0.0.1:6380> JSON.GET test .array
"[\"new\",99,2,3,4]"






 JSON.ARRINDEX查询数据坐标, JSON.ARRLEN获取数据长度

127.0.0.1:6380> JSON.GET test .array
"[\"new\",99,2,3,4]"
127.0.0.1:6380> JSON.ARRINDEX test .array 4     // 查找数据对应的坐标, 存在多个时返回第一个坐标
(integer) 3
127.0.0.1:6380> JSON.ARRINDEX test .array 99
(integer) 1

127.0.0.1:6380> JSON.ARRLEN test .array    // 获取数据长度
(integer) 4

 使用JSON.ARRPOP弹出数据

127.0.0.1:6380> JSON.ARRPOP test .array 1    // 弹出对应位置的数据
"99"
127.0.0.1:6380> JSON.GET test .array
"[\"new\",2,3,4]"
127.0.0.1:6380> JSON.ARRPOP test .array 6    // index 大于长度时, 弹出最后一个数据
"4"

 使用JSON.ARRTRIM裁剪数据

127.0.0.1:6380> JSON.SET test .array "[\"new\",2,3,4,5,6]"
OK
127.0.0.1:6380> JSON.GET test .array
"[\"new\",2,3,4,5,6]"
127.0.0.1:6380> JSON.ARRTRIM test .array 1 4    // 修建数组, 只保留指定范围内数据
(integer) 4
127.0.0.1:6380> JSON.GET test .array
"[2,3,4,5]"

对象

JSON.OBJKEYS 和 JSON.OBJLEN

127.0.0.1:6380> JSON.GET test
"{\"key\":\"valuealo\",\"n\":400,\"array\":[2,3,4,5]}"
127.0.0.1:6380> JSON.OBJKEYS test
1) "key"
2) "n"
3) "array"
127.0.0.1:6380> JSON.OBJLEN test
(integer) 3

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐