Skip to content

Commit b0f7499

Browse files
Merge pull request #32 from RedisInsight/main
new 2.42 tutorials
2 parents 92229ad + 4d1cde0 commit b0f7499

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+3181
-572
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
with:
2222
repo_token: "${{ secrets.GITHUB_TOKEN }}"
2323
prerelease: false
24-
automatic_release_tag: 2.x.x
24+
automatic_release_tag: 2.42
2525
files: |
2626
data.zip
2727
build.json

src/_scripts/manual.txt

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/ds/hashes.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Lists, sets, and sorted sets are great for many use cases, but the hash data type is on a higher level. Redis hashes are maps (sequences of field-value pairs) and are used to represent objects in Redis. Consider the following example that shows how to create a hash key using the `HSET` command.
2+
3+
Redis hashes are record types that are structured as name-value pairs. Consider the following example that shows how to create a hash key using the `HSET` command.
4+
5+
```redis:[run_confirmation=true] Create a hash
6+
HSET bike:1 model Deimos brand Ergonom type "Enduro bikes" price 4972
7+
```
8+
9+
`HSET` returns the number of added name-value pairs.
10+
11+
To retrieve the stored data, use the `HGETALL` command.
12+
13+
```redis HGETALL usage
14+
HGETALL bike:1 // returns all the name-value pairs associated with the key
15+
```
16+
17+
If you only want the values of a subset of the fields, use the `HGET` command.
18+
19+
```redis HGET usage
20+
HGET bike:1 price
21+
```
22+
23+
You can update fields in a hash using `HSET` by specifying a subset of its name-value pairs.
24+
25+
```redis:[run_confirmation=true] Update an existing hash
26+
HSET bike:1 model "Kraken" price 3000
27+
```
28+
29+
Use the `HDEL` command to delete one or more fields from a hash. Once all fields are removed, the hash key itself will be deleted.
30+
31+
```redis:[run_confirmation=true] Delete hash fields and keys
32+
HDEL bike:1 model
33+
HGETALL bike:1
34+
HDEL bike:1 brand type price
35+
HGETALL bike:1
36+
EXISTS bike:1
37+
```
38+
39+
Integer values in hash keys can be incremented or decremented in the same way as simple string keys using the `HINCRBY` command.
40+
The increment value must be a positive or negative integer.
41+
42+
```redis Hash INCRBY usage
43+
HINCRBY bike:1 price 100
44+
HINCRBY bike:1 price -100
45+
```
46+
47+
See [here](https://redis.io/docs/data-types/hashes?utm_source=redisinsight&utm_medium=main&utm_campaign=tutorials) for the hash type reference page, and [here](https://redis.io/commands/?group=hash?utm_source=redisinsight&utm_medium=main&utm_campaign=tutorials) for the entire set of Redis hash commands.

src/ds/json/adv-jsonpath.md

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
[JSONPath](https://goessner.net/articles/JsonPath/) expressions help you access specific elements within a JSON document, which is similar to how XPATH works for XML documents.
2+
JSONPath support was added to Redis Stack in version 2.0.
3+
Before that, [a legacy form of pathing](https://redis.io/docs/data-types/json/path/#legacy-path-syntax) was supported.
4+
Only JSONPath will be discussed in this tutorial.
5+
6+
You've already seen several examples of JSONPath in previous parts of this tutorial. The next sections will describe JSONPath in more complete detail.
7+
Some simple JSON documents will be used to demonstrate JSONPath's features.
8+
9+
```redis:[run_confirmation=true] Load documents
10+
JSON.SET lit1 $ 5
11+
JSON.SET lit2 $ '"abc"'
12+
JSON.SET lit3 $ true
13+
JSON.SET lit4 $ null
14+
JSON.SET obj1 $ '{"a":1, "b":2}'
15+
JSON.SET obj2 $ '{"a":[1,2,3,"a","b","c",false,true,["a",1],{"a":1},{"b":null}], "b":5}'
16+
JSON.SET arr1 $ [1,2,3]
17+
JSON.SET arrmap $ '[{"a":1}, {"b":2}]'
18+
```
19+
20+
**Note**:
21+
>A JSONPath query can resolve to multiple absolute path matches. When more than one matching path is identified, the JSON commands apply the operation to every possible path.
22+
23+
## JSONPath syntax
24+
25+
| Syntax element | Description |
26+
|----------------|-------------|
27+
| `$` | The root (outermost JSON element), starts the path. |
28+
| `.` | Selects a child element. |
29+
| `..` | Recursively descends through the JSON document. |
30+
| `*` | If current node is an array, `*` resolves to all array elements. If current node is an object, `*` resolves to all the object's members |
31+
| `[]` | Subscript operator, accesses an array element. |
32+
| `[,]` | Union, selects multiple elements. |
33+
| `[start:end:step]` | Array slice where `start`, `end`, and `step` are indexes. |
34+
| `[?(...)]` | Filters a JSON object or array. Supports comparison operators (`==`, `!=`, `<`, `<=`, `>`, `>=`, `=~`), logical operators (`&&`, `\|\|`), and parentheses for grouping (`(`, `)`). |
35+
| `@` | The current element, used in filter or script expressions. |
36+
37+
## Basic usage
38+
39+
`$` represents the root of a document. When `$` is combined with `.` and/or `[]`, a child element is selected. Here are a few examples.
40+
41+
```redis $ by itself
42+
JSON.GET obj1 $ // returns the entire obj1 document
43+
```
44+
45+
```redis Select an element with $.
46+
JSON.GET obj2 $.a // returns the array pointed to by a
47+
```
48+
49+
```redis Select an element with $[...]
50+
JSON.GET arr1 $[1] // returns the second element of the arr1 array
51+
```
52+
53+
```redis Select $.a using $.[] notation
54+
JSON.GET obj2 '$.["a"]' // note the use of single quotes in this expression; they're needed to be able to use double quotes in the expression
55+
```
56+
57+
It is possible to use `$` as part of a field name.
58+
59+
```redis:[run_confirmation=true] Using $ as part of a field name
60+
JSON.SET d $ '{"$":5, "$$":6}'
61+
JSON.GET d $.$ // returns "[5]"
62+
```
63+
64+
The use of quotes is optional when double quotes are not in use inside of an expression.
65+
Each of the following JSONPath expressions return the same value, `"[5]"`.
66+
67+
```redis Quote usage
68+
JSON.GET obj2 $.b
69+
JSON.GET obj2 $.'b'
70+
JSON.GET obj2 $'.b'
71+
```
72+
73+
**Note**:
74+
>If the current node is not an object or has no member named after the operand, an empty array is returned.
75+
76+
```redis Invalid path
77+
JSON.GET obj1 $.c // "[]" is returned
78+
```
79+
80+
```redis Combining . and []
81+
JSON.GET arrmap $[0].a
82+
```
83+
84+
## Bracket (union) expressions
85+
86+
The following syntaxes are supported:
87+
88+
- `'$["value1", "value2", ...]'`
89+
- `$[idx1, idx2, idx3, ...]`
90+
91+
Each represents a union operation, allowing you to select multiple elements.
92+
93+
For objects, return values are as follows:
94+
95+
- If the current node is an object, an array containing the values of the object’s fields, based on their names, is returned.
96+
- If the current node is an object and has no members named in the bracket expression, it is skipped.
97+
This could result in an empty array being returned.
98+
- If the current node is not an object, an empty array is returned.
99+
100+
Here are some examples:
101+
102+
```redis Bracket union examples with objects
103+
JSON.GET obj1 '$["a"]'
104+
JSON.GET obj1 '$["b","b"]'
105+
JSON.GET obj1 '$["a","c","b","d"]'
106+
JSON.GET obj1 '$["c","d"]'
107+
JSON.GET arr1 '$["a","b"]'
108+
```
109+
110+
For arrays:
111+
112+
- idx must be an integer
113+
- if idx < 0, it is replaced with min(0, idx + array-length)
114+
- if idx ≥ array-length (after normalization) - it is skipped
115+
116+
Return values are as follows:
117+
118+
- If the current node is an array, an array containing elements based on one or more 0-based indexes is returned.
119+
- If the current node is not an array, it is skipped, possibly resulting in an empty array being returned.
120+
121+
idx must be an integer
122+
if idx < 0, it is replaced with min(0, idx + array-length)
123+
if idx ≥ array-length (after normalization) - it is skipped
124+
125+
```redis Bracket union examples with arrays
126+
JSON.GET obj2 $.a[0]
127+
JSON.GET obj2 $.a[-1]
128+
JSON.GET obj2 $.a[100]
129+
JSON.GET obj2 $.a[-100]
130+
JSON.GET obj2 $.a[8][1]
131+
JSON.GET obj2 $.a[0,0,1,1]
132+
JSON.GET obj2 $.b[0]
133+
JSON.GET obj2 $.*[0]
134+
```
135+
136+
Redis Stack also supports slice syntax for arrays: `[start:`end`:`step`]`, where `start`, `end`, and `step` are indexes.
137+
If the current node is an array, an array containing elements extracted from an array are returned, based on a `start` index, an `end` index, and a `step` index.
138+
Array indexes are zero-based; the first element is index 0. Start Index is inclusive; End index is not inclusive.
139+
The following rules apply:
140+
141+
| Predicate | Rule |
142+
| --------- | ---- |
143+
| If `start` is specified | it must be an integer |
144+
| if `start` is omitted | it is replaced with 0 |
145+
| if `start` < 0 | it is replaced with min(0, `start` + array-length) |
146+
| if `start` > array-length | an empty array is returned |
147+
| if `end` is specified | it must be an integer |
148+
| If `end` is omitted | it is replaced with array-length |
149+
| If `end` ≥ array-length | it is replaced with array-length |
150+
| if `end` < 0 | it is replaced with min(0, `end` + array-length) |
151+
| If `end``start` (after normalization) | an empty array is returned |
152+
| If `step` is specified | it must be a positive integer |
153+
| If `step` is omitted | it is replaced with 1 |
154+
| If the current node in not an array | an empty array is returned |
155+
156+
```redis Array slice examples
157+
JSON.GET arr1 $[:]
158+
JSON.GET arr1 $[::]
159+
JSON.GET arr1 $[::2]
160+
JSON.GET arr1 $[0:1]
161+
JSON.GET arr1 $[0:2]
162+
JSON.GET lit3 $.*[:]
163+
```
164+
165+
## Wildcard expressions
166+
167+
The `*` character is a wildcard that expands depending on the type of the current node:
168+
169+
- If the current node is an array, an array containing the values of all the array’s elements is returned.
170+
171+
```redis Using * when the current node is an array
172+
JSON.GET arr1 $.* // each of the following commands return identical results
173+
JSON.GET arr1 $[*]
174+
JSON.GET arr1 $.[*]
175+
```
176+
177+
- If the current node is an object, an array containing the values of all the object’s members is returned.
178+
179+
```redis Using * when the current node is an object
180+
JSON.GET obj1 $.* // each of the following commands return identical results
181+
JSON.GET obj1 $[*]
182+
JSON.GET obj1 $.[*]
183+
```
184+
185+
- If current node is a literal, an empty array is returned.
186+
187+
```redis Using * when the current node is a literal
188+
JSON.GET lit1 $.*
189+
```

src/ds/json/arrays.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Redis has several commands that allow you to manipulate JSON arrays. Simple documents will be used to demonstrate each command.
2+
3+
```redis:[run_confirmation=true] 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:[run_confirmation=true] 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:[run_confirmation=true] 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:[run_confirmation=true] Trim $.a to just the first 3 elements
34+
JSON.ARRTRIM 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:[run_confirmation=true] Re-create the document
41+
JSON.SET doc $ '{"a": [10, 20, 30, 40, 50]}'
42+
```
43+
44+
```redis:[run_confirmation=true] Trim to the last two values
45+
JSON.ARRTRIM 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:[run_confirmation=true] 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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
## Create JSON documents
2+
3+
Here's a query that creates a JSON document describing a single bike.
4+
5+
```redis:[run_confirmation=true] 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": "The Jigger is the best ride for the smallest of tikes!",
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:[run_confirmation=true] 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\": \"A light mountain bike for kids.\", \"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\": \"A lighter, more durable mountain bike for six years and older.\", \"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.

0 commit comments

Comments
 (0)