redisjsonpathnode-redisredisjson

Is it possible to use JSONPath with "?()" syntax to filter objects (not array) in Redis?


I'm using node-redis library. I have redis key: test1 with value:

{
    "requests": [
        {
            "requestId": "123",
            "requestType": "TYPE_1",
            "foo": "bar",
            "timestampS": 1230011000
        },
        {
            "requestId": "124",
            "requestType": "TYPE_1",
            "foo": "bar2",
            "timestampS": 1230011050
        }
    ]
}

To get all requests with timestampS < 1230011040, I need to run:

const value = await this.client.json.get('test1', { path: `$.*[?(@.timestampS<1230011040)]` });

It works. The value is an array with one element (request object with "requestId": "123").

However, I don't know how to get an array with all request objects with timestampS < 1230011040 from redis key test2 with value:

{
  "123": {
        "requestId": "123",
        "requestType": "TYPE_1",
        "foo": "bar",
        "timestampS": 1230011000
    },
    "124": {
        "requestId": "124",
        "requestType": "TYPE_1",
        "foo": "bar2",
        "timestampS": 1230011050
    }
}

Is it possible?


Solution

  • It took me a some time, but the solution was easy. I just had to remove the unnecessary star (*) in the JSONPath. This path: $.[?(@.timestampS<1230011040)] works.