RedisJSON icon indicating copy to clipboard operation
RedisJSON copied to clipboard

add sorted nested array supported and other redis data structure nested supported

Open someview opened this issue 2 years ago • 2 comments

We have faced with some difficulties, let's move on to creating a group chat structure using redis:

{
   "roomId": "123",
   "Notice": "456",
   "MaxNum": "100",
   "Members":[
       {locZone:1, active: 1000},
       {locZone:2, active: 1002},
       {locZone:3, active: 1001},
      ...
   ]
   "Msgs": redis-list
}

We want to exec crud on sub arrarys. eg: we also want to get active members, just like mongodb: "gt:{$Members.active:1001}" . Now it seems impossbile for RedisJson. Is there some cutpoint to do this?

someview avatar Sep 26 '23 09:09 someview

You can get sub-array members based on some filtering expression:

redis> JSON.SET k $ '{"roomId":"123", "Notice":"456", "MaxNum":"100", "Members":[{"locZone":1, "active":1000}, {"locZone":2, "active":1002}, {"locZone":3, "active":1001}]}'
OK
redis> JSON.GET k '$.Members[[email protected]==1001]'
"[{\"locZone\":3,\"active\":1001}]"
redis> JSON.GET k '$.Members[[email protected]>1]'
"[{\"locZone\":2,\"active\":1002},{\"locZone\":3,\"active\":1001}]"
redis> JSON.GET k '$.Members[[email protected]>1 && @.active==1001]'
"[{\"locZone\":3,\"active\":1001}]"

Is this what you're looking for?

LiorKogan avatar Sep 26 '23 09:09 LiorKogan

You can get sub-array members based on some filtering expression:

redis> JSON.SET k $ '{"roomId":"123", "Notice":"456", "MaxNum":"100", "Members":[{"locZone":1, "active":1000}, {"locZone":2, "active":1002}, {"locZone":3, "active":1001}]}'
OK
redis> JSON.GET k '$.Members[[email protected]==1001]'
"[{\"locZone\":3,\"active\":1001}]"
redis> JSON.GET k '$.Members[[email protected]>1]'
"[{\"locZone\":2,\"active\":1002},{\"locZone\":3,\"active\":1001}]"
redis> JSON.GET k '$.Members[[email protected]>1 && @.active==1001]'
"[{\"locZone\":3,\"active\":1001}]"

Is this what you're looking for? This may be expensive if we often do some op with this document: the nested field may need the sorted array, just like sorted set in redis. Actually, some document features are powerful. eg: sub array nested document update, aggregate pipeline like mongodb:

// the matchCondition like this: {$Members.locZone: 1}、
// the SetOp like this:{'$set': { 'documents.$.active' :88} }
db.coll.update(matchCondition,  SetOp)

We may do some simple aggregate operation on document, eg: pick the above Members locZone list where the active > 1000.

someview avatar Sep 26 '23 16:09 someview