RedisJSON的支持与安装
要求:
● Redis6+
● JSON不能超过128层
RedisJson
docker run -p 6379:6379 --name redis-stack redis/redis-stack:latest
redis-server --loadmodule ./target/release/librejson.so
https://redis.io/docs/latest/develop/data-types/json/path/
原始数据
{
"inventory": {
"headphones": [
{
"id": 12345,
"name": "Noise-cancelling Bluetooth headphones",
"description": "Wireless Bluetooth headphones with noise-cancelling technology",
"wireless": true,
"connection": "Bluetooth",
"price": 99.98,
"stock": 25,
"free-shipping": false,
"colors": ["black", "silver"]
},
{
"id": 12346,
"name": "Wireless earbuds",
"description": "Wireless Bluetooth in-ear headphones",
"wireless": true,
"connection": "Bluetooth",
"price": 64.99,
"stock": 17,
"free-shipping": false,
"colors": ["black", "white"]
},
{
"id": 12347,
"name": "Mic headset",
"description": "Headset with built-in microphone",
"wireless": false,
"connection": "USB",
"price": 35.01,
"stock": 28,
"free-shipping": false
}
],
"keyboards": [
{
"id": 22345,
"name": "Wireless keyboard",
"description": "Wireless Bluetooth keyboard",
"wireless": true,
"connection": "Bluetooth",
"price": 44.99,
"stock": 23,
"free-shipping": false,
"colors": ["black", "silver"]
},
{
"id": 22346,
"name": "USB-C keyboard",
"description": "Wired USB-C keyboard",
"wireless": false,
"connection": "USB-C",
"price": 29.99,
"stock": 30,
"free-shipping": false
}
]
}
}
JSON.SET store $ '{"inventory":{"headphones":[{"id":12345,"name":"Noise-cancelling Bluetooth headphones","description":"Wireless Bluetooth headphones with noise-cancelling technology","wireless":true,"connection":"Bluetooth","price":99.98,"stock":25,"free-shipping":false,"colors":["black","silver"]},{"id":12346,"name":"Wireless earbuds","description":"Wireless Bluetooth in-ear headphones","wireless":true,"connection":"Bluetooth","price":64.99,"stock":17,"free-shipping":false,"colors":["black","white"]},{"id":12347,"name":"Mic headset","description":"Headset with built-in microphone","wireless":false,"connection":"USB","price":35.01,"stock":28,"free-shipping":false}],"keyboards":[{"id":22345,"name":"Wireless keyboard","description":"Wireless Bluetooth keyboard","wireless":true,"connection":"Bluetooth","price":44.99,"stock":23,"free-shipping":false,"colors":["black","silver"]},{"id":22346,"name":"USB-C keyboard","description":"Wired USB-C keyboard","wireless":false,"connection":"USB-C","price":29.99,"stock":30,"free-shipping":false}]}}'
新增数据
JSON.GET store $.inventory.*
"[[{\"id\":12345,\"name\":\"Noise-cancelling Bluetooth headphones\",\"description\":\"Wireless Bluetooth headphones with noise-cancelling technology\",\"wireless\":true,\"connection\":\"Bluetooth\",\"price\":99.98,\"stock\":25,\"free-shipping\":false,\"colors\":[\"black\",\"silver\"]},{\"id\":12346,\"name\":\"Wireless earbuds\",\"description\":\"Wireless Bluetooth in-ear headphones\",\"wireless\":true,\"connection\":\"Bluetooth\",\"price\":64.99,\"stock\":17,\"free-shipping\":false,\"colors\":[\"black\",\"white\"]},{\"id\":12347,\"name\":\"Mic headset\",\"description\":\"Headset with built-in microphone\",\"wireless\":false,\"connection\":\"USB\",\"price\":35.01,\"stock\":28,\"free-shipping\":false}],[{\"id\":22345,\"name\":\"Wireless keyboard\",\"description\":\"Wireless Bluetooth keyboard\",\"wireless\":true,\"connection\":\"Bluetooth\",\"price\":44.99,\"stock\":23,\"free-shipping\":false,\"colors\":[\"black\",\"silver\"]},{\"id\":22346,\"name\":\"USB-C keyboard\",\"description\":\"Wired USB-C keyboard\",\"wireless\":false,\"connection\":\"USB-C\",\"price\":29.99,\"stock\":30,\"free-shipping\":false}]]"
获取所有headphones下的人名
JSON.GET store $.inventory.headphones[*].name
"[\"Noise-cancelling Bluetooth headphones\",\"Wireless earbuds\",\"Mic headset\"]"
JSON.GET store '$.inventory["headphones"][*].name'
"[\"Noise-cancelling Bluetooth headphones\",\"Wireless earbuds\",\"Mic headset\"]"
JSON.GET store $..headphones[*].name
"[\"Noise-cancelling Bluetooth headphones\",\"Wireless earbuds\",\"Mic headset\"]"
得到所有name
JSON.GET store $..name
"[\"Noise-cancelling Bluetooth headphones\",\"Wireless earbuds\",\"Mic headset\",\"Wireless keyboard\",\"USB-C keyboard\"]"
得到所有headphones第1-3个节点下的name属性
JSON.GET store $..headphones[0:2].name
"[\"Noise-cancelling Bluetooth headphones\",\"Wireless earbuds\"]"
属性筛选
127.0.0.1:6379> JSON.GET store $.inventory.*[?(@.free-shipping==true)].name
"[\"Noise-cancelling Bluetooth headphones\",\"Wireless earbuds\"]"