Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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



Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ We can now search in the address:

### References

- [Indexing JSON Document](https://oss.redis.com/redisearch/master/Indexing_JSON/)
- [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/)


Original file line number Diff line number Diff line change
Expand Up @@ -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.redis.com/redisjson/) in the Quickstart tutorial.
- [How to store and retrieve nested JSON document](/howtos/redisjson/storing-complex-json-document)
Expand Down
Original file line number Diff line number Diff line change
@@ -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]
---


Expand Down Expand Up @@ -434,6 +434,6 @@ 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.redis.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


1 change: 1 addition & 0 deletions docs/howtos/redisjson/using-go/index-usinggo.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ 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)
Expand Down
1 change: 1 addition & 0 deletions docs/howtos/redisjson/using-java/index-usingjava.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
141 changes: 81 additions & 60 deletions docs/howtos/redisjson/using-nodejs/index-usingnodejs.mdx
Original file line number Diff line number Diff line change
@@ -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.redis.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://redis.com/blog/index-and-query-json-docs-with-redis/)
3 changes: 2 additions & 1 deletion docs/howtos/redisjson/using-python/index-usingpython.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.redis.com/redisjson/) in the Quickstart tutorial.
- Learn more about [RedisJSON](https://redisjson.io) in the Quickstart tutorial.


Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ 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)
Expand Down
1 change: 1 addition & 0 deletions docs/howtos/redisjson/using-ruby/index-usingruby.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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/)