|
1 | 1 | # @node-redis/json
|
2 |
| -The sources and docs for this package are in the main [node-redis](https://github.com/redis/node-redis) repo. |
| 2 | + |
| 3 | +This package provides support for the [RedisJSON](https://redisjson.io) module, which adds JSON as a native data type to Redis. It extends the [Node Redis client](https://) to include functions for each of the RedisJSON commands. |
| 4 | + |
| 5 | +To use these extra commands, your Redis server must have the RedisJSON module installed. |
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +For a complete example, see [`managing-json.js`](https://github.com/redis/node-redis/blob/master/examples/managing-json.js) in the Node Redis examples folder. |
| 10 | + |
| 11 | +### Storing JSON Documents in Redis |
| 12 | + |
| 13 | +The [`JSON.SET`](https://oss.redis.com/redisjson/commands/#jsonset) command stores a JSON value at a given JSON Path in a Redis key. |
| 14 | + |
| 15 | +Here, we'll store a JSON document in the root of the Redis key "`mydoc`": |
| 16 | + |
| 17 | +```javascript |
| 18 | +import { createClient } from 'redis'; |
| 19 | + |
| 20 | +... |
| 21 | +await client.json.set('noderedis:jsondata', '$', { |
| 22 | + name: 'Roberta McDonald', |
| 23 | + pets: [ |
| 24 | + { |
| 25 | + name: 'Rex', |
| 26 | + species: 'dog', |
| 27 | + age: 3, |
| 28 | + isMammal: true |
| 29 | + }, |
| 30 | + { |
| 31 | + name: 'Goldie', |
| 32 | + species: 'fish', |
| 33 | + age: 2, |
| 34 | + isMammal: false |
| 35 | + } |
| 36 | + ] |
| 37 | +}); |
| 38 | +``` |
| 39 | + |
| 40 | +For more information about RedisJSON's path syntax, [check out the documentation](https://oss.redis.com/redisjson/path/). |
| 41 | + |
| 42 | +### Retrieving JSON Documents from Redis |
| 43 | + |
| 44 | +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: |
| 45 | + |
| 46 | +```javascript |
| 47 | +const results = await client.json.get('noderedis:jsondata', { |
| 48 | + path: [ |
| 49 | + '.pets[1].name', |
| 50 | + '.pets[1].age' |
| 51 | + ] |
| 52 | +}); |
| 53 | +``` |
| 54 | + |
| 55 | +`results` will contain the following: |
| 56 | + |
| 57 | +```javascript |
| 58 | + '.pets[1].name': 'Goldie', '.pets[1].age': 2 } |
| 59 | +``` |
| 60 | + |
| 61 | +### Performing Atomic Updates on JSON Documents Stored in Redis |
| 62 | + |
| 63 | +RedisJSON includes commands that can atomically update values in a JSON document, in place in Redis without having to first retrieve the entire document. |
| 64 | + |
| 65 | +Using the [`JSON.NUMINCRBY`](https://oss.redis.com/redisjson/commands/#jsonnumincrby) command, we can update the age of one of the pets like this: |
| 66 | + |
| 67 | +```javascript |
| 68 | +await client.json.numIncrBy('noderedis:jsondata', '.pets[1].age', 1); |
| 69 | +``` |
| 70 | + |
| 71 | +And we can add a new object to the pets array with the [`JSON.ARRAPPEND`](https://oss.redis.com/redisjson/commands/#jsonarrappend) command: |
| 72 | + |
| 73 | +```javascript |
| 74 | + await client.json.arrAppend('noderedis:jsondata', '.pets', { |
| 75 | + name: 'Robin', |
| 76 | + species: 'bird', |
| 77 | + age: 1, |
| 78 | + isMammal: false |
| 79 | + }); |
| 80 | +``` |
0 commit comments