Skip to content

Commit a67ac02

Browse files
author
Simon Prickett
authored
Merge pull request #185 from redis-developer/update-node-json-cache-redis-4
Update RedisJSON for Node.js howto for Node Redis 4
2 parents 2b9d566 + 31e4886 commit a67ac02

File tree

11 files changed

+98
-70
lines changed

11 files changed

+98
-70
lines changed

docs/howtos/redisjson/getting-started/index-gettingstarted.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,9 @@ A JSON object can also have another object. Here is a simple example of a JSON o
279279
"{\"firstName\":\"Alpha\",\"lastName\":\"K\",\"age\":23,\"address\":{\"streetAddress\":\"110 Fulbourn Road Cambridge\",\"city\":\"San Francisco\",\"state\":\"California\",\"postalCode\":\"94016\"}}"
280280
```
281281

282-
### Next Step
282+
### Next Steps
283283

284+
- [RU204: Storing, Querying and Indexing JSON at Speed](https://university.redis.com/courses/ru204/) - a course at Redis University
284285
- [RedisJSON and Python](/howtos/redisjson/using-python)
285286
- [How to store and retrieve nested JSON document](/howtos/redisjson/storing-complex-json-document)
286287
- [Importing JSON data into Redis using NodeJS](/howtos/redisjson/using-nodejs)

docs/howtos/redisjson/json-using-redisearch/jsonindex-document/index-jsonindex-document.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ RediSearch has been providing indexing and search capabilities on hashes. Under
1414

1515
By exposing its capabilities to other modules, RedisJSON gives RediSearch the ability to index JSON documents so users can now find documents by indexing and querying the content. These combined modules give you a powerful, low latency, JSON-oriented document database!
1616

17-
### Prerequisite:
17+
### Prerequisites:
1818

1919
- Redis 6.x or later
2020
- RediSearch 2.2 or later
@@ -181,6 +181,7 @@ Please Note: It is not possible to index JSON object and JSON arrays.
181181

182182
### References
183183

184+
- [RU204: Storing, Querying and Indexing JSON at Speed](https://university.redis.com/courses/ru204/) - a course at Redis University
184185

185186

186187

docs/howtos/redisjson/jsonind-document/index-jsonind-document.mdx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ We can now search in the address:
210210

211211
### References
212212

213-
- [Indexing JSON Document](https://oss.redis.com/redisearch/master/Indexing_JSON/)
213+
- [RU204: Storing, Querying and Indexing JSON at Speed](https://university.redis.com/courses/ru204/) - a course at Redis University
214+
- [Indexing JSON Documents](https://oss.redis.com/redisearch/master/Indexing_JSON/)
214215
- [Indexing, Querying, and Full-Text Search of JSON Documents with Redis](https://redis.com/blog/index-and-query-json-docs-with-redis/)
215-
216-

docs/howtos/redisjson/storing-complex-json-document/index-storing-complex-json-document.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ In case you want to fetch specific filed (like address), then the code would loo
141141

142142
### References
143143

144+
- [RU204: Storing, Querying and Indexing JSON at Speed](https://university.redis.com/courses/ru204/) - a course at Redis University
144145
- [Importing JSON data into Redis using NodeJS](/howtos/redisjson/using-nodejs)
145146
- Learn more about [RedisJSON](https://oss.redis.com/redisjson/) in the Quickstart tutorial.
146147
- [How to store and retrieve nested JSON document](/howtos/redisjson/storing-complex-json-document)

docs/howtos/redisjson/storing-json-using-nodejs/index-storingjson-nodejs.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
22
id: index-storingjson-nodejs
3-
title: How to store and retrieve JSON documents using NodeJS
4-
sidebar_label: Storing and retrieving JSON documents using NodeJS
3+
title: How to store and retrieve JSON documents using Node.js
4+
sidebar_label: Storing and retrieving JSON documents using Node.js
55
slug: /howtos/redisjson/storing-json-using-nodejs
6-
authors: [ajeet]
6+
authors: [ajeet, simon]
77
---
88

99

@@ -434,6 +434,6 @@ If you have the correct JSON path in your code, visiting `http://localhost:80801
434434
- [How to store and retrieve nested JSON document](/howtos/redisjson/storing-complex-json-document)
435435
- [Importing JSON data into Redis using NodeJS](/howtos/redisjson/using-nodejs)
436436
- Learn more about [RedisJSON](https://oss.redis.com/redisjson/) in the Quickstart tutorial.
437-
437+
- [RU204: Storing, Querying and Indexing JSON at Speed](https://university.redis.com/courses/ru204/) - a course at Redis University
438438

439439

docs/howtos/redisjson/using-go/index-usinggo.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ Follow the below steps to get started with RedisJSON using Go client.
129129
### References
130130

131131
- [Go and Redis](/develop/golang/)
132+
- [RU204: Storing, Querying and Indexing JSON at Speed](https://university.redis.com/courses/ru204/) - a course at Redis University
132133
- [RedisJSON and Python](/howtos/redisjson/using-python)
133134
- [How to store and retrieve nested JSON document](/howtos/redisjson/storing-complex-json-document)
134135
- [Importing JSON data into Redis using NodeJS](/howtos/redisjson/using-nodejs)

docs/howtos/redisjson/using-java/index-usingjava.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Use the "clean" command to delete all previously compiled Java .class files and
9696

9797
### References
9898

99+
- [RU204: Storing, Querying and Indexing JSON at Speed](https://university.redis.com/courses/ru204/) - a course at Redis University
99100
- [RedisJSON and Python](/howtos/redisjson/using-python)
100101
- [How to store and retrieve nested JSON document](/howtos/redisjson/storing-complex-json-document)
101102
- [Importing JSON data into Redis using NodeJS](/howtos/redisjson/using-nodejs)
Lines changed: 81 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,129 @@
11
---
22
id: index-usingnodejs
3-
title: How to cache JSON data in Redis with NodeJS
4-
sidebar_label: RedisJSON and NodeJS
3+
title: How to cache JSON data in Redis with Node.js
4+
sidebar_label: RedisJSON and Node.js
55
slug: /howtos/redisjson/using-nodejs
6-
authors: [ajeet]
6+
authors: [ajeet,simon]
77
---
88

9-
Node.js has become incredibly popular for both web and mobile application development. It is a runtime for JavaScript so it can run JavaScript code on the machine. Node.js can be installed on MacOS, Linux and Windows system. The Node Package Manager (npm) enables developers to reuse useful codes which are tried and tested and helps you to build strong and steady applications quickly.
9+
Node.js has become incredibly popular for both web and mobile application development. Node.js can be installed on MacOS, Linux and Windows systems. The Node Package Manager (npm) enables developers to install packages which are tried and tested libraries that help you to build applications quickly.
1010

11-
Node.js is a fast web framework, but adding the power, speed and flexibility of Redis can take it to the next level.Redis is best suited to situations that require data to be retrieved and delivered to the client as quickly as possible
12-
If you’re using Node, you can use the node-redis module to interact with Redis. If you’re using RediJSON, you can use redis-rejson module to interact with RedisJSON.
11+
Node.js is a fast runtime, but adding the power, speed and flexibility of Redis can take it to the next level. Redis is best suited to situations that require data to be retrieved and delivered to the client as quickly as possible.
1312

14-
The 'RedisJSON Module plugin for node_redis' package allows node_redis (2.8+) to interface with the Redis module RedisJSON.To use this module, you will need Redis 4.0 or higher and the redisjson module installed.
13+
[RedisJSON](https://redisjson.io) is an add-on module that adds JSON as a native data type to Redis. It enables atomic, in place operations to be performed on JSON documents stored in Redis.
1514

16-
Follow the below steps to get started with RedisJSON and Node.js:
15+
We'll use the [node-redis](https://npmjs.com/package/redis) client to connect to Redis and leverage the power of RedisJSON.
1716

18-
### Step 1. Run RedisMod Docker container
19-
20-
This simple container image bundles together the latest stable releases of Redis and select Redis modules from Redis Lab.
17+
### Step 1. Run the redismod Docker Container
2118

19+
This simple container image bundles together the latest stable releases of Redis and select Redis modules from Redis, Inc.
2220

2321
```bash
24-
docker run -d -p 6379:6379 redislabs/redismod:latest
22+
$ docker run -d -p 6379:6379 redislabs/redismod:latest
2523
```
2624

25+
### Step 2. Install Node.js
2726

28-
### Step 2. Install Node
27+
Download and install the current LTS (Long Term Support) version of Node.js from the [nodejs.org](https://nodejs.org/) website.
2928

30-
```bash
31-
brew install node
32-
```
29+
### Step 3. Initialize an npm Project
3330

34-
### Step 3. Install Redis
31+
Run `npm init` to initialize a new project. Use the default answers to all the questions:
3532

36-
Node Redis is a high performance Node.js Redis client.
33+
```
34+
$ mkdir jsondemo
35+
$ cd jsondemo
36+
$ npm init
37+
```
3738

38-
```bash
39-
npm install redis
39+
Now edit `package.json` and add the line `"type": "module"`. The file should look something like this:
40+
41+
```json
42+
{
43+
"name": "jsondemo",
44+
"type": "module",
45+
"version": "1.0.0",
46+
"description": "",
47+
"main": "index.js",
48+
"scripts": {
49+
"test": "echo \"Error: no test specified\" && exit 1"
50+
},
51+
"author": "",
52+
"license": "ISC"
53+
}
4054
```
4155

42-
### Step 4. Install RedisJSON module
56+
### Step 4. Install node-redis
57+
58+
[node-redis](https://npmjs.com/package/redis) is a high performance Node.js Redis client with support for the RedisJSON module. Install it using `npm`:
4359

4460
```bash
45-
npm -i redis-rejson
61+
$ npm install redis
4662
```
4763

48-
### Step 5. Create a file
64+
### Step 5. Create a JavaScript File
4965

50-
Copy the below content and save the file as “app.js
66+
Copy the code below into a file called `app.js`:
5167

68+
```javascript
69+
import { createClient } from 'redis';
5270

53-
```bash
54-
const redis=require("redis");
55-
rejson = require('redis-rejson');
56-
57-
rejson(redis); /* important - this must come BEFORE creating the client */
58-
let client= redis.createClient({
59-
port:6379,
60-
host:'localhost'
61-
});
62-
63-
let test_node_key = 'test_node';
64-
client.json_set(test_node_key, '.', '{"node":4303}', function (err) {
65-
if (err) { throw err; }
66-
console.log('Set JSON at key ' + test_node_key + '.');
67-
client.json_get(test_node_key, '.node', function (err, value) {
68-
if (err) { throw err; }
69-
console.log('value of node:', value); //outputs 4303
70-
client.quit();
71-
});
72-
});
73-
```
71+
async function redisJSONDemo () {
72+
try {
73+
const TEST_KEY = 'test_node';
7474

75+
const client = createClient();
76+
await client.connect();
7577

78+
// RedisJSON uses JSON Path syntax. '.' is the root.
79+
await client.json.set(TEST_KEY, '.', { node: 4303 });
80+
const value = await client.json.get(TEST_KEY, {
81+
// JSON Path: .node = the element called 'node' at root level.
82+
path: '.node'
83+
});
7684

85+
console.log(`value of node: ${value}`);
7786

78-
### Step 6. Run the app
87+
await client.quit();
88+
} catch (e) {
89+
console.error(e);
90+
}
91+
}
7992

80-
```bash
81-
node app.js
93+
redisJSONDemo();
8294
```
8395

96+
### Step 5. Run the Application
97+
98+
Start the application as follows:
8499

85100
```bash
86-
Set JSON at key test_node.
87-
value of node: 4303
101+
$ node app.js
88102
```
89103

104+
You should see this output:
105+
106+
```bash
107+
value of node: 4303
90108
```
91-
% redis-cli
92-
127.0.0.1:6379> monitor
93-
OK
94109

95-
1628071593.564178 [0 172.17.0.1:65054] "info"
96-
1628071593.567058 [0 172.17.0.1:65054] "json.set" "test_node" "." "{\"node\":4303}"
97-
1628071593.572035 [0 172.17.0.1:65054] "json.get" "test_node" ".node"
110+
Using the Redis [`MONITOR`](https://redis.io/commands/monitor) command, you can see the Redis commands that node-redis sent to the Redis server while running the application:
111+
98112
```
113+
$ redis-cli
114+
127.0.0.1:6379> monitor
115+
OK
99116
117+
1637866932.281949 [0 127.0.0.1:61925] "JSON.SET" "test_node" "." "{\"node\":4303}"
118+
1637866932.282842 [0 127.0.0.1:61925] "JSON.GET" "test_node" ".node"
119+
```
100120

101121
### References
102122

123+
- [RU204: Storing, Querying and Indexing JSON at Speed](https://university.redis.com/courses/ru204/) - a course at Redis University
103124
- [RedisJSON and Python](/howtos/redisjson/using-python)
104-
- [How to store and retrieve nested JSON document](/howtos/redisjson/storing-complex-json-document)
105-
- [Importing JSON data into Redis using NodeJS](/howtos/redisjson/using-nodejs)
106-
- Learn more about [RedisJSON](https://oss.redis.com/redisjson/) in the Quickstart tutorial.
107-
- [How to build shopping cart app using NodeJS and RedisJSON](/howtos/shoppingcart)
125+
- [How to store and retrieve nested JSON documents](/howtos/redisjson/storing-complex-json-document)
126+
- [Importing JSON data into Redis using Node.js](/howtos/redisjson/using-nodejs)
127+
- Learn more about [RedisJSON](https://redisjson.io/) in the Quickstart tutorial.
128+
- [How to build a shopping cart app using Node.js and RedisJSON](/howtos/shoppingcart)
108129
- [Indexing, Querying, and Full-Text Search of JSON Documents with Redis](https://redis.com/blog/index-and-query-json-docs-with-redis/)

docs/howtos/redisjson/using-python/index-usingpython.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,9 @@ In the above example, the code tries to fetch only the location under the addres
138138

139139
### References
140140

141+
- [RU204: Storing, Querying and Indexing JSON at Speed](https://university.redis.com/courses/ru204/) - a course at Redis University
141142
- [How to cache JSON data in Redis with Python](/howtos/redisjson/using-python)
142143
- [Importing JSON data into Redis using NodeJS](/howtos/redisjson/using-nodejs)
143-
- Learn more about [RedisJSON](https://oss.redis.com/redisjson/) in the Quickstart tutorial.
144+
- Learn more about [RedisJSON](https://redisjson.io) in the Quickstart tutorial.
144145

145146

docs/howtos/redisjson/using-redisinsight/index-usingredisinsight.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Select "employee_profile" to display the JSON data
9292

9393
### References
9494

95+
- [RU204: Storing, Querying and Indexing JSON at Speed](https://university.redis.com/courses/ru204/) - a course at Redis University
9596
- [RedisJSON and Python](/howtos/redisjson/using-python)
9697
- [How to store and retrieve nested JSON document](/howtos/redisjson/storing-complex-json-document)
9798
- [Importing JSON data into Redis using NodeJS](/howtos/redisjson/using-nodejs)

0 commit comments

Comments
 (0)