Skip to content

Commit cd2458d

Browse files
authored
Document adding a new child with JSON.SET (RedisJSON#622)
* Document adding a new child with JSON.SET * Restoring previous edits * Restoring previous edits (2)
1 parent 49e752b commit cd2458d

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

docs/commands.md

+24-2
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ JSON.SET <key> <path> <json>
4444

4545
Sets the JSON value at `path` in `key`.
4646

47-
For new Redis keys, the `path` must be the root. For existing keys, replaces the current value with the `json` value if the entire `path` exists.
47+
For new Redis keys the `path` must be the root. For existing keys, when the entire `path` exists, the value that it contains is replaced with the `json` value. For existing keys, when the `path` exists, except for the last element, a new child is added with the `json` value.
4848

49-
Adds a key (with its respective value) to a JSON Object (in a RedisJSON data type key) only if it is the last child in the `path`. The optional subcommands modify this behavior for both new RedisJSON data type keys as well as the JSON Object keys in them:
49+
Adds a key (with its respective value) to a JSON Object (in a RedisJSON data type key) only if it is the last child in the `path`, or it is the parent of a new child being added in the `path`. The optional subcommands modify this behavior for both new RedisJSON data type keys as well as the JSON Object keys in them:
5050

5151
* `NX` - only set the key if it does not already exist
5252
* `XX` - only set the key if it already exists
@@ -56,6 +56,28 @@ Adds a key (with its respective value) to a JSON Object (in a RedisJSON data typ
5656
[Simple String][1] - `OK` if executed correctly, or [Null Bulk][3] if the specified `NX` or `XX`
5757
conditions were not met.
5858

59+
60+
#### Examples:
61+
Replacing an existing value
62+
```sql
63+
127.0.0.1:6379> JSON.SET doc $ '{"a":2}'
64+
OK
65+
127.0.0.1:6379> JSON.SET doc $.a '3'
66+
OK
67+
127.0.0.1:6379> JSON.GET doc $
68+
"[{\"a\":3}]"
69+
```
70+
71+
Adding a new value
72+
```sql
73+
127.0.0.1:6379> JSON.SET doc $ '{"a":2}'
74+
OK
75+
127.0.0.1:6379> JSON.SET doc $.b '8'
76+
OK
77+
127.0.0.1:6379> JSON.GET doc $
78+
"[{\"a\":2,\"b\":8}]"
79+
```
80+
5981
### JSON.GET
6082

6183
> **Available since 1.0.0.**

0 commit comments

Comments
 (0)