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?
2
Answers
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.In terms of JSON Path, yes, the filter selector operates on both stays and objects. For objects, it ignores the keys and iterates through the values.
I don’t know whether redis supports that, but it’s a pretty basic feature, so it should.
Also, when in doubt, just try it.