From 4b4e0f84832cecffbd8245bef3b062806393ee72 Mon Sep 17 00:00:00 2001 From: Simon Prickett Date: Thu, 25 Nov 2021 17:54:15 +0000 Subject: [PATCH 1/3] Fixed NodeJS -> Node.js, added Simon as co author as this is taken from ioredis crash course --- .../storing-json-using-nodejs/index-storingjson-nodejs.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/howtos/redisjson/storing-json-using-nodejs/index-storingjson-nodejs.mdx b/docs/howtos/redisjson/storing-json-using-nodejs/index-storingjson-nodejs.mdx index 015b14e4ab..bd1ea3e3a4 100644 --- a/docs/howtos/redisjson/storing-json-using-nodejs/index-storingjson-nodejs.mdx +++ b/docs/howtos/redisjson/storing-json-using-nodejs/index-storingjson-nodejs.mdx @@ -1,9 +1,9 @@ --- id: index-storingjson-nodejs -title: How to store and retrieve JSON documents using NodeJS -sidebar_label: Storing and retrieving JSON documents using NodeJS +title: How to store and retrieve JSON documents using Node.js +sidebar_label: Storing and retrieving JSON documents using Node.js slug: /howtos/redisjson/storing-json-using-nodejs -authors: [ajeet] +authors: [ajeet, simon] --- From 65838257101f0bd3834a6a3b948dd833e0a93ad7 Mon Sep 17 00:00:00 2001 From: Simon Prickett Date: Thu, 25 Nov 2021 18:40:43 +0000 Subject: [PATCH 2/3] Adds RU204 reference, fixes some redislabs URLs. --- .../redisjson/getting-started/index-gettingstarted.mdx | 3 ++- .../jsonindex-document/index-jsonindex-document.mdx | 3 ++- .../redisjson/jsonind-document/index-jsonind-document.mdx | 5 +++-- .../index-storing-complex-json-document.mdx | 1 + .../storing-json-using-nodejs/index-storingjson-nodejs.mdx | 2 ++ docs/howtos/redisjson/using-go/index-usinggo.mdx | 3 ++- docs/howtos/redisjson/using-java/index-usingjava.mdx | 1 + docs/howtos/redisjson/using-python/index-usingpython.mdx | 3 ++- .../redisjson/using-redisinsight/index-usingredisinsight.mdx | 3 ++- docs/howtos/redisjson/using-ruby/index-usingruby.mdx | 1 + 10 files changed, 18 insertions(+), 7 deletions(-) diff --git a/docs/howtos/redisjson/getting-started/index-gettingstarted.mdx b/docs/howtos/redisjson/getting-started/index-gettingstarted.mdx index 8c465ed008..05b7a8d24f 100644 --- a/docs/howtos/redisjson/getting-started/index-gettingstarted.mdx +++ b/docs/howtos/redisjson/getting-started/index-gettingstarted.mdx @@ -279,8 +279,9 @@ A JSON object can also have another object. Here is a simple example of a JSON o "{\"firstName\":\"Alpha\",\"lastName\":\"K\",\"age\":23,\"address\":{\"streetAddress\":\"110 Fulbourn Road Cambridge\",\"city\":\"San Francisco\",\"state\":\"California\",\"postalCode\":\"94016\"}}" ``` -### Next Step +### Next Steps +- [RU204: Storing, Querying and Indexing JSON at Speed](https://university.redis.com/courses/ru204/) - a course at Redis University - [RedisJSON and Python](/howtos/redisjson/using-python) - [How to store and retrieve nested JSON document](/howtos/redisjson/storing-complex-json-document) - [Importing JSON data into Redis using NodeJS](/howtos/redisjson/using-nodejs) diff --git a/docs/howtos/redisjson/json-using-redisearch/jsonindex-document/index-jsonindex-document.mdx b/docs/howtos/redisjson/json-using-redisearch/jsonindex-document/index-jsonindex-document.mdx index 32d9ea6723..706b92917c 100644 --- a/docs/howtos/redisjson/json-using-redisearch/jsonindex-document/index-jsonindex-document.mdx +++ b/docs/howtos/redisjson/json-using-redisearch/jsonindex-document/index-jsonindex-document.mdx @@ -14,7 +14,7 @@ RediSearch has been providing indexing and search capabilities on hashes. Under 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! -### Prerequisite: +### Prerequisites: - Redis 6.x or later - RediSearch 2.2 or later @@ -181,6 +181,7 @@ Please Note: It is not possible to index JSON object and JSON arrays. ### References +- [RU204: Storing, Querying and Indexing JSON at Speed](https://university.redis.com/courses/ru204/) - a course at Redis University diff --git a/docs/howtos/redisjson/jsonind-document/index-jsonind-document.mdx b/docs/howtos/redisjson/jsonind-document/index-jsonind-document.mdx index 04e91068df..efa205cad0 100644 --- a/docs/howtos/redisjson/jsonind-document/index-jsonind-document.mdx +++ b/docs/howtos/redisjson/jsonind-document/index-jsonind-document.mdx @@ -210,7 +210,8 @@ We can now search in the address: ### References -- [Indexing JSON Document](https://oss.redislabs.com/redisearch/master/Indexing_JSON) -- [Indexing, Querying, and Full-Text Search of JSON Documents with Redis](https://redislabs.com/blog/index-and-query-json-docs-with-redis/) +- [RU204: Storing, Querying and Indexing JSON at Speed](https://university.redis.com/courses/ru204/) - a course at Redis University +- [Indexing JSON Documents](https://oss.redis.com/redisearch/master/Indexing_JSON/) +- [Indexing, Querying, and Full-Text Search of JSON Documents with Redis](https://redis.com/blog/index-and-query-json-docs-with-redis/) diff --git a/docs/howtos/redisjson/storing-complex-json-document/index-storing-complex-json-document.mdx b/docs/howtos/redisjson/storing-complex-json-document/index-storing-complex-json-document.mdx index 6b12332c05..287b99a5c5 100644 --- a/docs/howtos/redisjson/storing-complex-json-document/index-storing-complex-json-document.mdx +++ b/docs/howtos/redisjson/storing-complex-json-document/index-storing-complex-json-document.mdx @@ -141,6 +141,7 @@ In case you want to fetch specific filed (like address), then the code would loo ### References +- [RU204: Storing, Querying and Indexing JSON at Speed](https://university.redis.com/courses/ru204/) - a course at Redis University - [Importing JSON data into Redis using NodeJS](/howtos/redisjson/using-nodejs) - Learn more about [RedisJSON](https://oss.redislabs.com/redisjson/) in the Quickstart tutorial. - [How to store and retrieve nested JSON document](/howtos/redisjson/storing-complex-json-document) diff --git a/docs/howtos/redisjson/storing-json-using-nodejs/index-storingjson-nodejs.mdx b/docs/howtos/redisjson/storing-json-using-nodejs/index-storingjson-nodejs.mdx index bd1ea3e3a4..3eedab0c40 100644 --- a/docs/howtos/redisjson/storing-json-using-nodejs/index-storingjson-nodejs.mdx +++ b/docs/howtos/redisjson/storing-json-using-nodejs/index-storingjson-nodejs.mdx @@ -434,6 +434,8 @@ If you have the correct JSON path in your code, visiting `http://localhost:80801 - [How to store and retrieve nested JSON document](/howtos/redisjson/storing-complex-json-document) - [Importing JSON data into Redis using NodeJS](/howtos/redisjson/using-nodejs) - Learn more about [RedisJSON](https://oss.redislabs.com/redisjson/) in the Quickstart tutorial. +- [RU204: Storing, Querying and Indexing JSON at Speed](https://university.redis.com/courses/ru204/) - a course at Redis University + diff --git a/docs/howtos/redisjson/using-go/index-usinggo.mdx b/docs/howtos/redisjson/using-go/index-usinggo.mdx index 41d8a6cd9e..cea1ece1ab 100644 --- a/docs/howtos/redisjson/using-go/index-usinggo.mdx +++ b/docs/howtos/redisjson/using-go/index-usinggo.mdx @@ -129,12 +129,13 @@ Follow the below steps to get started with RedisJSON using Go client. ### References - [Go and Redis](/develop/golang/) +- [RU204: Storing, Querying and Indexing JSON at Speed](https://university.redis.com/courses/ru204/) - a course at Redis University - [RedisJSON and Python](/howtos/redisjson/using-python) - [How to store and retrieve nested JSON document](/howtos/redisjson/storing-complex-json-document) - [Importing JSON data into Redis using NodeJS](/howtos/redisjson/using-nodejs) - Learn more about [RedisJSON](https://oss.redislabs.com/redisjson/) in the Quickstart tutorial. - [How to build shopping cart app using NodeJS and RedisJSON](/howtos/shoppingcart) -- [Indexing, Querying, and Full-Text Search of JSON Documents with Redis](https://redislabs.com/blog/index-and-query-json-docs-with-redis/) +- [Indexing, Querying, and Full-Text Search of JSON Documents with Redis](https://redis.com/blog/index-and-query-json-docs-with-redis/) diff --git a/docs/howtos/redisjson/using-java/index-usingjava.mdx b/docs/howtos/redisjson/using-java/index-usingjava.mdx index c431e88bc5..f8106160fd 100644 --- a/docs/howtos/redisjson/using-java/index-usingjava.mdx +++ b/docs/howtos/redisjson/using-java/index-usingjava.mdx @@ -96,6 +96,7 @@ Use the "clean" command to delete all previously compiled Java .class files and ### References +- [RU204: Storing, Querying and Indexing JSON at Speed](https://university.redis.com/courses/ru204/) - a course at Redis University - [RedisJSON and Python](/howtos/redisjson/using-python) - [How to store and retrieve nested JSON document](/howtos/redisjson/storing-complex-json-document) - [Importing JSON data into Redis using NodeJS](/howtos/redisjson/using-nodejs) diff --git a/docs/howtos/redisjson/using-python/index-usingpython.mdx b/docs/howtos/redisjson/using-python/index-usingpython.mdx index 9db6b826e8..8da97145c9 100644 --- a/docs/howtos/redisjson/using-python/index-usingpython.mdx +++ b/docs/howtos/redisjson/using-python/index-usingpython.mdx @@ -138,8 +138,9 @@ In the above example, the code tries to fetch only the location under the addres ### References +- [RU204: Storing, Querying and Indexing JSON at Speed](https://university.redis.com/courses/ru204/) - a course at Redis University - [How to cache JSON data in Redis with Python](/howtos/redisjson/using-python) - [Importing JSON data into Redis using NodeJS](/howtos/redisjson/using-nodejs) -- Learn more about [RedisJSON](https://oss.redislabs.com/redisjson/) in the Quickstart tutorial. +- Learn more about [RedisJSON](https://redisjson.io) in the Quickstart tutorial. diff --git a/docs/howtos/redisjson/using-redisinsight/index-usingredisinsight.mdx b/docs/howtos/redisjson/using-redisinsight/index-usingredisinsight.mdx index 6df9470606..76f70d7a8c 100644 --- a/docs/howtos/redisjson/using-redisinsight/index-usingredisinsight.mdx +++ b/docs/howtos/redisjson/using-redisinsight/index-usingredisinsight.mdx @@ -92,9 +92,10 @@ Select "employee_profile" to display the JSON data ### References +- [RU204: Storing, Querying and Indexing JSON at Speed](https://university.redis.com/courses/ru204/) - a course at Redis University - [RedisJSON and Python](/howtos/redisjson/using-python) - [How to store and retrieve nested JSON document](/howtos/redisjson/storing-complex-json-document) - [Importing JSON data into Redis using NodeJS](/howtos/redisjson/using-nodejs) - Learn more about [RedisJSON](https://oss.redislabs.com/redisjson/) in the Quickstart tutorial. - [How to build shopping cart app using NodeJS and RedisJSON](/howtos/shoppingcart) -- [Indexing, Querying, and Full-Text Search of JSON Documents with Redis](https://redislabs.com/blog/index-and-query-json-docs-with-redis/) +- [Indexing, Querying, and Full-Text Search of JSON Documents with Redis](https://redis.com/blog/index-and-query-json-docs-with-redis/) diff --git a/docs/howtos/redisjson/using-ruby/index-usingruby.mdx b/docs/howtos/redisjson/using-ruby/index-usingruby.mdx index 9c9cb9240e..6fa4430d7d 100644 --- a/docs/howtos/redisjson/using-ruby/index-usingruby.mdx +++ b/docs/howtos/redisjson/using-ruby/index-usingruby.mdx @@ -76,6 +76,7 @@ You can verify what's happening in the background by running the monitor command ### References +- [RU204: Storing, Querying and Indexing JSON at Speed](https://university.redis.com/courses/ru204/) - a course at Redis University - [Rate Limiting app in Ruby and Redis](/howtos/ratelimiting/) - [Ruby and Redis](/develop/ruby/) From 882c7f2ec5746860456703a3796e7dd494740846 Mon Sep 17 00:00:00 2001 From: Simon Prickett Date: Thu, 25 Nov 2021 19:07:06 +0000 Subject: [PATCH 3/3] Updated for Node Redis 4. --- .../using-nodejs/index-usingnodejs.mdx | 141 ++++++++++-------- 1 file changed, 81 insertions(+), 60 deletions(-) diff --git a/docs/howtos/redisjson/using-nodejs/index-usingnodejs.mdx b/docs/howtos/redisjson/using-nodejs/index-usingnodejs.mdx index ec457c332e..76c3cc5c00 100644 --- a/docs/howtos/redisjson/using-nodejs/index-usingnodejs.mdx +++ b/docs/howtos/redisjson/using-nodejs/index-usingnodejs.mdx @@ -1,108 +1,129 @@ --- id: index-usingnodejs -title: How to cache JSON data in Redis with NodeJS -sidebar_label: RedisJSON and NodeJS +title: How to cache JSON data in Redis with Node.js +sidebar_label: RedisJSON and Node.js slug: /howtos/redisjson/using-nodejs -authors: [ajeet] +authors: [ajeet,simon] --- -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. +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. -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 -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. +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. -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. +[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. -Follow the below steps to get started with RedisJSON and Node.js: +We'll use the [node-redis](https://npmjs.com/package/redis) client to connect to Redis and leverage the power of RedisJSON. -### Step 1. Run RedisMod Docker container - -This simple container image bundles together the latest stable releases of Redis and select Redis modules from Redis Lab. +### Step 1. Run the redismod Docker Container +This simple container image bundles together the latest stable releases of Redis and select Redis modules from Redis, Inc. ```bash - docker run -d -p 6379:6379 redislabs/redismod:latest +$ docker run -d -p 6379:6379 redislabs/redismod:latest ``` +### Step 2. Install Node.js -### Step 2. Install Node +Download and install the current LTS (Long Term Support) version of Node.js from the [nodejs.org](https://nodejs.org/) website. -```bash - brew install node -``` +### Step 3. Initialize an npm Project -### Step 3. Install Redis +Run `npm init` to initialize a new project. Use the default answers to all the questions: -Node Redis is a high performance Node.js Redis client. +``` +$ mkdir jsondemo +$ cd jsondemo +$ npm init +``` -```bash - npm install redis +Now edit `package.json` and add the line `"type": "module"`. The file should look something like this: + +```json +{ + "name": "jsondemo", + "type": "module", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC" +} ``` -### Step 4. Install RedisJSON module +### Step 4. Install node-redis + +[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`: ```bash - npm -i redis-rejson +$ npm install redis ``` -### Step 5. Create a file +### Step 5. Create a JavaScript File -Copy the below content and save the file as “app.js” +Copy the code below into a file called `app.js`: +```javascript +import { createClient } from 'redis'; -```bash - const redis=require("redis"); - rejson = require('redis-rejson'); - - rejson(redis); /* important - this must come BEFORE creating the client */ - let client= redis.createClient({ - port:6379, - host:'localhost' - }); - - let test_node_key = 'test_node'; - client.json_set(test_node_key, '.', '{"node":4303}', function (err) { - if (err) { throw err; } - console.log('Set JSON at key ' + test_node_key + '.'); - client.json_get(test_node_key, '.node', function (err, value) { - if (err) { throw err; } - console.log('value of node:', value); //outputs 4303 - client.quit(); - }); - }); -``` +async function redisJSONDemo () { + try { + const TEST_KEY = 'test_node'; + const client = createClient(); + await client.connect(); + // RedisJSON uses JSON Path syntax. '.' is the root. + await client.json.set(TEST_KEY, '.', { node: 4303 }); + const value = await client.json.get(TEST_KEY, { + // JSON Path: .node = the element called 'node' at root level. + path: '.node' + }); + console.log(`value of node: ${value}`); -### Step 6. Run the app + await client.quit(); + } catch (e) { + console.error(e); + } +} -```bash - node app.js +redisJSONDemo(); ``` +### Step 5. Run the Application + +Start the application as follows: ```bash - Set JSON at key test_node. - value of node: 4303 +$ node app.js ``` +You should see this output: + +```bash +value of node: 4303 ``` - % redis-cli - 127.0.0.1:6379> monitor - OK - 1628071593.564178 [0 172.17.0.1:65054] "info" - 1628071593.567058 [0 172.17.0.1:65054] "json.set" "test_node" "." "{\"node\":4303}" - 1628071593.572035 [0 172.17.0.1:65054] "json.get" "test_node" ".node" +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: + ``` +$ redis-cli +127.0.0.1:6379> monitor +OK +1637866932.281949 [0 127.0.0.1:61925] "JSON.SET" "test_node" "." "{\"node\":4303}" +1637866932.282842 [0 127.0.0.1:61925] "JSON.GET" "test_node" ".node" +``` ### References +- [RU204: Storing, Querying and Indexing JSON at Speed](https://university.redis.com/courses/ru204/) - a course at Redis University - [RedisJSON and Python](/howtos/redisjson/using-python) -- [How to store and retrieve nested JSON document](/howtos/redisjson/storing-complex-json-document) -- [Importing JSON data into Redis using NodeJS](/howtos/redisjson/using-nodejs) -- Learn more about [RedisJSON](https://oss.redislabs.com/redisjson/) in the Quickstart tutorial. -- [How to build shopping cart app using NodeJS and RedisJSON](/howtos/shoppingcart) +- [How to store and retrieve nested JSON documents](/howtos/redisjson/storing-complex-json-document) +- [Importing JSON data into Redis using Node.js](/howtos/redisjson/using-nodejs) +- Learn more about [RedisJSON](https://redisjson.io/) in the Quickstart tutorial. +- [How to build a shopping cart app using Node.js and RedisJSON](/howtos/shoppingcart) - [Indexing, Querying, and Full-Text Search of JSON Documents with Redis](https://redislabs.com/blog/index-and-query-json-docs-with-redis/)