![](/img/trans.png)
[英]Reading JSON data with nested obects within arrays to SQL Server 2016
[英]Manipulating nested array of obects in RedisJSON
我有一個帶有嵌套數組的 JSON,如下所示,要保存在 Redis 中。 我正在使用 RedisJSON 模塊將數據保存為 JSON。
customer:12345 : {
info : {
key1: val1,
key2: val2,
key3: val3
},
rides: [
{
rideid: xxx,
from: fromval,
to: toval,
date: dateofride,
distance: distanceval,
points: pointsval
},
{
rideid: yyy,
from: fromval,
to: toval,
date: dateofride,
distance: distanceval,
points: pointsval
},
...
]
}
我有一種情況,可以將新項目添加到數組中,也可以編輯現有項目。 我正在使用帶有 express.js 的 node-redis 客戶端。 Express 應用程序僅接收在游樂設施數組中更改或添加的數據。 如果該項已經在數組中,則必須用新數據替換現有的數組項(rideid 是每個對象的鍵),否則必須將其添加到數組中。 我如何實現這一目標?
給定以下 JSON
{
"info": {
"key1": "val1"
},
"rides": [{
"rideid": "xxx",
"points": 0
},
{
"rideid": "yyy",
"points": 10
}
]
}
使用以下命令在 RedisJSON 中設置關鍵customer:12345
127.0.0.1:6379> JSON.SET customer:12345 . "{\"info\": {\"key1\": \"val1\",\"key2\": \"val2\",\"key3\": \"val3\"},\"rides\":[{\"rideid\": \"xxx\",\"points\": 0 },\t{\"rideid\": \"yyy\",\"points\": 10}]}"
您可以通過例如將分數增加 5 來更新rided yyy
,如下所示
127.0.0.1:6379> JSON.NUMINCRBY customer:12345 "$.rides[?(@.rideid=='yyy')].points" 5
"[15]"
$.rides[?(@.rideid=='yyy')].points
是一個 JSONPath 表達式(更多here和here也在這里回答)
添加新行程
127.0.0.1:6379> JSON.ARRAPPEND customer:12345 $.rides "{\"rideid\": \"zzz\",\"points\": 5 }"
1) (integer) 3
所有 RedisJSON 命令都可以在這里找到
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.