Skip to content

Commit 14f4f79

Browse files
committed
DOC-3179: RI tutorial restructure (part C)
1 parent 6386197 commit 14f4f79

File tree

6 files changed

+268
-13
lines changed

6 files changed

+268
-13
lines changed

src/ds/json/arrays.md

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,54 @@
1-
## Placeholder
1+
Redis has several commands that allow you to manipulate JSON arrays. Simple documents will be used to demonstrate each command.
22

3-
This is placeholder content.
3+
```redis Create a document
4+
JSON.SET doc $ '{"a": [10, 20, 30, 40, 50]}'
5+
```
6+
7+
- `JSON.ARRLEN` - get the length of an array:
8+
9+
```redis Get the length of $.a
10+
JSON.ARRLEN doc $.a
11+
```
12+
13+
- `JSON.ARRAPPEND` - append one or more values to an array:
14+
15+
```redis Append two values to $.a
16+
JSON.ARRAPPEND doc $.a 60 '"foo"'
17+
JSON.GET doc $.a
18+
```
19+
20+
- `JSON.ARRPOP` - remove an element from an array:
21+
22+
```redis Remove the last item from $.a
23+
JSON.ARRPOP doc $.a
24+
JSON.GET doc $.a
25+
```
26+
27+
Note: `JSON.ARRPOP` takes two optional arguments: a path and a position. If no path is given, then `$` is assumed.
28+
Position is zero-based, with negative numbers representing last to first.
29+
For example, `-1` mean the last element and `-2` means the second to last element.
30+
31+
- `JSON.ARRTRIM` - trim an array so that it contains only the specified inclusive range of elements.
32+
33+
```redis Trim $.a to just the first 3 elements
34+
JSON.TRIM doc $.a 0 2
35+
JSON.GET doc
36+
```
37+
38+
Now, reset doc to it's original value and trim `$.a` to just the last two values:
39+
40+
```redis Re-create the document
41+
JSON.SET doc $ '{"a": [10, 20, 30, 40, 50]}'
42+
```
43+
44+
```redis Trim to the last two values
45+
JSON.TRIM doc $.a -2 -1
46+
JSON.GET doc
47+
```
48+
49+
As discussed earlier in this tutorial, `JSON.MERGE` can be used to replace entire arrays.
50+
51+
```redis Replace $.a with a different set of values
52+
JSON.MERGE doc $.a '["a", "b", "c"]'
53+
JSON.GET doc
54+
```

src/ds/json/create.md

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,76 @@
1-
## Placeholder
1+
## Create JSON documents
22

3-
This is placeholder content.
3+
Here's a query that creates a JSON document describing a single bike.
4+
5+
```redis Create a JSON document
6+
JSON.SET bicycle:1 $ '{
7+
"model": "Jigger",
8+
"brand": "Velorim",
9+
"price": 270,
10+
"type": "Kids bikes",
11+
"specs": {
12+
"material": "aluminium",
13+
"weight": "10"
14+
},
15+
"description": "Small and powerful, the Jigger is the best ride for the smallest of tikes! This is the tiniest kids\u2019 pedal bike on the market available without a coaster brake, the Jigger is the vehicle of choice for the rare tenacious little rider raring to go. We say rare because this smokin\u2019 little bike is not ideal for a nervous first-time rider, but it\u2019s a true giddy up for a true speedster. The Jigger is a 12 inch lightweight kids bicycle and it will meet your little one\u2019s need for speed. It\u2019s a single speed bike that makes learning to pump pedals simple and intuitive. It even has a handle in the bottom of the saddle so you can easily help your child during training! The Jigger is among the most lightweight children\u2019s bikes on the planet. It is designed so that 2-3 year-olds fit comfortably in a molded ride position that allows for efficient riding, balanced handling and agility. The Jigger\u2019s frame design and gears work together so your buddingbiker can stand up out of the seat, stop rapidly, rip over trails and pump tracks. The Jigger\u2019s is amazing on dirt or pavement. Your tike will speed down the bike path in no time. The Jigger will ship with a coaster brake. A freewheel kit is provided at no cost.",
16+
"addons": [
17+
"reflectors",
18+
"grip tassles"
19+
],
20+
"helmet_included": false
21+
}'
22+
```
23+
24+
Now retrieve the newly created JSON document.
25+
26+
```redis Retrieve bicycle:1
27+
JSON.GET bicycle:1
28+
```
29+
30+
In the above example, the path, which is root (`$`), is implied. You could also write this command as:
31+
32+
```
33+
JSON.GET bicycle:1 $
34+
```
35+
36+
You can also retrieve parts of documents using JSONPath expressions. JSONPath will be discussed in more detail later in this tutorial, but here are a few examples:
37+
38+
```redis Get the price of bicycle:1
39+
JSON.GET bicycle:1 $.price
40+
```
41+
42+
```redis Get the weight of bicycle:1
43+
JSON.GET bicycle:1 $.specs.weight
44+
```
45+
46+
```redis Get the first addon of bicycle:1
47+
JSON.GET bicycle:1 $.addons[0]
48+
```
49+
50+
You can create multiple documents in a single command using `JSON.MSET`:
51+
52+
```redis Add two more documents using JSON.MGET
53+
JSON.MSET "bicycle:2" $ "{\"model\": \"Hillcraft\", \"brand\": \"Bicyk\", \"price\": 1200, \"type\": \"Kids Mountain Bikes\", \"specs\": {\"material\": \"carbon\", \"weight\": \"11\"}, \"description\": \"Kids want to ride with as little weight as possible. Especially on an incline! They may be at the age when a 27.5\\\" wheel bike is just too clumsy coming off a 24\\\" bike. The Hillcraft 26 is just the solution they need! Imagine 120mm travel. Boost front/rear. You have NOTHING to tweak because it is easy to assemble right out of the box. The Hillcraft 26 is an efficient trail trekking machine. Up or down does not matter - dominate the trails going both down and up with this amazing bike. The name Monarch comes from Monarch trail in Colorado where we love to ride. It\u2019s a highly technical, steep and rocky trail but the rip on the waydown is so fulfilling. Don\u2019t ride the trail on a hardtail! It is so much more fun on the full suspension Hillcraft! Hit your local trail with the Hillcraft Monarch 26 to get to where you want to go.\", \"addons\": [\"reflectors\", \"safety lights\"],\"helmet_included\": false}" "bicycle:3" $ "{\"model\": \"Chook air 5\", \"brand\": \"Nord\", \"price\": 815, \"type\": \"Kids Mountain Bikes\", \"specs\": {\"material\": \"alloy\", \"weight\": \"9.1\"}, \"description\": \"The Chook Air 5 gives kids aged six years and older a durable and uberlight mountain bike for their first experience on tracks and easy cruising through forests and fields. The lower top tube makes it easy to mount and dismount in any situation, giving your kids greater safety on the trails. The Chook Air 5 is the perfect intro to mountain biking.\", \"addons\": [\"reflectors\", \"safety lights\"],\"helmet_included\": false}"
54+
```
55+
56+
You can retrieve multiple documents or parts of documents in a single command using `JSON.MGET`:
57+
58+
```redis Get bicycle:1 and bicycle:2
59+
JSON.MGET bicycle:1 bicycle:2 $
60+
```
61+
62+
```redis Get the price of all three bicycle documents
63+
JSON.MGET bicycle:1 bicycle:2 bicycle:3 $.price
64+
```
65+
66+
There are two other commands you can use to get information from documents:
67+
68+
```redis Get the length of bicycle:1's description
69+
JSON.STRLEN bicycle:1 $.description
70+
```
71+
72+
```redis Get the type of bicycle:1's helmet_included attribute
73+
JSON.TYPE bicycle:1 $.helmet_included
74+
```
75+
76+
The `JSON.MERGE` can also be used to create new documents. `JSON.MERGE` will be covered in more detail later in this tutorial.

src/ds/json/introduction.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
## Placeholder
1+
The JSON capability of Redis Stack provides JavaScript Object Notation (JSON) support for Redis, which allows Redis to function as a document database.
2+
It lets you store, update, and retrieve JSON values in a Redis database, similar to any other Redis data type. Redis JSON also works seamlessly with Search and Query to let you index and query JSON documents.
23

3-
This is placeholder content.
4+
Primary features include:
5+
6+
- Full support for the JSON standard.
7+
- [JSONPath](https://goessner.net/articles/JsonPath/) syntax for selecting/updating elements inside documents.
8+
- Documents are stored as binary data in a tree structure, allowing fast access to sub-elements.
9+
- Typed atomic operations for all JSON value types.

src/ds/json/learn-more.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
## Placeholder
2-
3-
This is placeholder content.
1+
You can read more about the JSON data type [here](https://redis.io/docs/data-types/json/). See [here](https://redis.io/commands/?group=json) for the complete list of JSON commands.

src/ds/json/modify.md

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,108 @@
1-
## Placeholder
1+
Modifying JSON documents is straightforward using a combination of JSONPath expressions and Redis's JSON command set.
22

3-
This is placeholder content.
3+
Here are some examples.
4+
5+
## Extend documents
6+
7+
```redis Add a new key:value pair to an existing document
8+
JSON.SET bicycle:1 $.newkey '"value"'
9+
```
10+
11+
You could also use `JSON.MSET` to create or update multiple documents at the same time. First, delete `$.newkey` from `bicycle:1` using `JSON.DEL`.
12+
13+
```redis Delete $.newkey from bicycle:1
14+
JSON.DEL bicycle:1 $.newkey
15+
```
16+
17+
Next, add `$.newkey` to all three bicycles:
18+
19+
```redis Add $.newkey too all three bicycles using JSON.MSET
20+
JSON.MSET bicycle:1 $.newkey '"value1"' bicycle:2 $.newkey '"value2"' bicycle:3 $.newkey '"value3"'
21+
JSON.MGET bicycle:1 bicycle:2 bicycle:3 $.newkey
22+
```
23+
24+
## Manipulate numeric values
25+
26+
There are two commands that allow you to perform arithmetic operations on components of documents:
27+
28+
- `JSON.NUMINCRBY` - adds a value to a numeric field.
29+
30+
```redis Decrease the price of bicycle:1
31+
JSON.GET bicycle:1 $.price
32+
JSON.NUMINCRBY bicycle:1 $.price -10
33+
JSON.GET bicycle:1 $.price
34+
```
35+
36+
- `JSON.NUMMULTBY` - multiply a numeric field by a value.
37+
38+
```redis Discount bicycle:1 by 10%
39+
JSON.NUMMULTBY bicycle:1 $.price 0.9
40+
```
41+
## Manipulate string and boolean values
42+
43+
Appending information to JSON strings is straightforward.
44+
45+
```redis Append a string bicycle:1's model
46+
JSON.STRAPPEND bicycle:1 $.model '"naut"'
47+
JSON.GET bicycle:1 $.model
48+
```
49+
50+
The `JSON.TOGGLE` command can be used to toggle boolean values.
51+
52+
```redis Toggle the value of bicycle:1's helmet_included value
53+
JSON.GET bicycle:1 $.helmet_included
54+
JSON.TOGGLE bicycle:1 $.helmet_included
55+
JSON.GET bicycle:1 $.helmet_included
56+
```
57+
58+
## Deeper document manipulation
59+
60+
As you saw earlier, the `JSON.MERGE` command can be used to create new documents. Additionally, it can also be used for the following use cases:
61+
62+
- Create a non-existant path-value:
63+
64+
```redis Add a new field-value pair to bicycle:1
65+
JSON.MERGE bicycle:1 $.newkey2 '"value 2 1"'
66+
JSON.GET bicycle:1
67+
```
68+
69+
- Replace an existing value:
70+
71+
```redis Change bicycle:1's model back to Jigger
72+
JSON.MERGE bicycle:1 $.model '"Jigger"'
73+
JSON.GET bicycle:1 $.model
74+
```
75+
76+
- Delete an existing value:
77+
78+
```redis Delete newkey2 from bicycle:1
79+
JSON.MERGE bicycle:1 $ '{"newkey2": null}'
80+
JSON.GET bicycle:1
81+
```
82+
83+
- Replace an array:
84+
85+
```redis Replace bicycle:1's addons
86+
JSON.MERGE bicycle:1 $.addons '["reflectors", "rainbow seat"]'
87+
JSON.GET bicycle:1 $.addons
88+
```
89+
90+
- Make changes to multiple paths (no example).
91+
92+
## Delete information
93+
94+
You can delete field-value pairs using the `JSON.DEL` or `JSON.FORGET` commands:
95+
96+
```redis Delete newkey from bicycle:1
97+
JSON.DEL bicycle:1 $.newkey
98+
JSON.GET bicycle:1
99+
```
100+
101+
The `JSON.CLEAR` command will empty all arrays and set all numeric values to zero.
102+
103+
```redis Clear out bicycle:1
104+
JSON.CLEAR bicycle:1
105+
JSON.GET bicycle:1
106+
```
107+
108+
As with all Redis keys, you can use the `DEL` command to delete keys entirely.

src/ds/json/objects.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1-
## Placeholder
1+
Redis provides two commands that operate specifically on JSON objects. A simple document will be used to demonstrate each of the two commands.
22

3-
This is placeholder content.
3+
```redis Create document
4+
JSON.SET doc $ '{"a":1,"b":2,"o":{"c":3,"d":4}}'
5+
```
6+
7+
- `JSON.OBJKEYS` - retrieve the fields of a JSON object.
8+
9+
```redis Get doc's fields
10+
JSON.OBJKEYS doc
11+
```
12+
13+
```redis Get the fields for $.o
14+
JSON.OBJKEYS doc $.o
15+
```
16+
17+
- `JSON.OBJLEN` - get the number of fields of a JSON object.
18+
19+
```redis Get the number of fields in the doc object
20+
JSON.OBJLEN doc
21+
```
22+
23+
```redis Get the number of fields in the $.o object
24+
JSON.OBJLEN doc $.o
25+
```

0 commit comments

Comments
 (0)