* churn(test): use redislabs/client-libs-test for testing This switches our testing infrastructure from redis/redis-stack to redislabs/client-libs-test Docker image across all packages. This change also updates the default Docker version from 7.4.0-v1 to 8.0-M04-pre. * churn(test): verify CONFIG SET / GET compatibility with Redis 8 - Add tests for Redis 8 search configuration settings - Deprecate Redis Search CONFIG commands in favor of standard CONFIG - Test read-only config restrictions for Redis 8 * churn(test): handle Redis 8 coordinate precision in GEOPOS - Update GEOPOS tests to handle increased precision in Redis 8 (17 decimal places vs 14) - Add precision-aware coordinate comparison helper - Add comprehensive test suite for coordinate comparison function * test(search): adapt SUGGET tests for Redis 8 empty results - Update tests to expect empty array ([]) instead of null for SUGGET variants - Affects sugGet, sugGetWithPayloads, sugGetWithScores, and sugGetWithScoresWithPayloads * test(search): support Redis 8 INFO indexes_all field - Add indexes_all field introduced in Redis 8 to index definition test * refactor!(search): simplify PROFILE commands to return raw response - BREAKING CHANGE: FT.PROFILE now returns raw response, letting users implement their own parsing * test: improve version-specific test coverage - Add `testWithClientIfVersionWithinRange` method to run tests for specific Redis versions - Refactor TestUtils to handle version comparisons more accurately - Update test utilities across Redis modules to run tests against multiple versions, and not against latest only
@redis/json
This package provides support for the RedisJSON module, which adds JSON as a native data type to Redis.
Should be used with redis
/@redis/client
.
⚠️ To use these extra commands, your Redis server must have the RedisJSON module installed.
Usage
For a complete example, see managing-json.js
in the examples folder.
Storing JSON Documents in Redis
The JSON.SET
command stores a JSON value at a given JSON Path in a Redis key.
Here, we'll store a JSON document in the root of the Redis key "mydoc
":
await client.json.set('noderedis:jsondata', '$', {
name: 'Roberta McDonald',
pets: [{
name: 'Rex',
species: 'dog',
age: 3,
isMammal: true
}, {
name: 'Goldie',
species: 'fish',
age: 2,
isMammal: false
}]
});
For more information about RedisJSON's path syntax, check out the documentation.
Retrieving JSON Documents from Redis
With RedisJSON, we can retrieve all or part(s) of a JSON document using the JSON.GET
command and one or more JSON Paths. Let's get the name and age of one of the pets:
const results = await client.json.get('noderedis:jsondata', {
path: [
'.pets[1].name',
'.pets[1].age'
]
});
results
will contain the following:
{ '.pets[1].name': 'Goldie', '.pets[1].age': 2 }
Performing Atomic Updates on JSON Documents Stored in Redis
RedisJSON includes commands that can atomically update values in a JSON document, in place in Redis without having to first retrieve the entire document.
Using the JSON.NUMINCRBY
command, we can update the age of one of the pets like this:
await client.json.numIncrBy('noderedis:jsondata', '.pets[1].age', 1);
And we can add a new object to the pets array with the JSON.ARRAPPEND
command:
await client.json.arrAppend('noderedis:jsondata', '.pets', {
name: 'Robin',
species: 'bird',
age: 1,
isMammal: false
});