Main features of RedisJson

Author : xuzhiping   2022-11-11 15:42:47 Browse: 1313
Category : JavaScript

Abstract: 1. Introduction to mongodb MongoDB is a database based on distributed file storage, it is written in C++language and aims to pro...

1. Introduction to mongodb

mongodb

MongoDB is a database based on distributed file storage, it is written in C++language and aims to provide scalable, and high-performance data NoSQL storage solutions for WEB applications. MongoDB is a product between relational databases and non relational databases. MongoDB is the most functional and similar to relational databases among non relational databases. MongoDB stores data as a document, and the data structure consists of key value (key=>value) pairs. MongoDB documents are similar to JSON objects. Field values can contain other documents, arrays, and document arrays.

MongoDB data storage

MongoDB can add more nodes under high load to ensure server performance. Since MongoDB is stored as JSON like documents, updating documents, deleting documents or obtaining documents are all operating on JSON like documents. We can take a look at MongoDB insertion. The following examples are taken from the network:

>db.col.insert({title: 'MongoDB', 
    description: 'MongoDB is a Nosql database',
    by: 'baidu',
    tags: ['mongodb', 'database', 'NoSQL'],
})

Query:

> db.col.find({"by":"baidu"}).pretty()
{
        "_id" : ObjectId("56063f17ade2f21f36b03133"),
        "description" : "MongoDB is a Nosql database",
        "by" : "baidu",
        "tags" : [
                "mongodb",
                "database",
                "NoSQL"
        ]
}

The data structure of this JSON-like document is basically the same as that of JSON. All data stored in the collection is in BSON format.

BSON is a binary storage format similar to JSON, it's short for Binary JSON.

2.RedisJson

RedisJSON - Redis JSON data type

One of Redis's latest features: support for JSON storage and retrieval. RedisJSON is a Redis module that implements the ECMA-404 JSON data exchange standard as the local data type. It allows you to store, update and obtain JSON values (documents) according to the Redis key.

Main features:

  • Full support for JSON standard;
  • Select elements in a document using JSONPath-like syntax;
  • The document is stored as binary data in a tree structure, and child elements can be accessed quickly;
  • Atomic operations are defined for all JSON value types.

RedisJSON was developed with < 3 in Redis. The source code is available from https://github.com/RedisJSON/RedisJSON.

Next, let's show you:

1.Start RedisJSON with Docker

Use Docker to run the following command on Windows, MacOS, or Linux:

Docker run-p 6379 6379-name redis-redisjson redislabs/rejson:latest

Print the log as follows:

Print log

2.Use RedisJSON

Redis cli can directly access:

//Insert json data,the key is doc, value is json
➜  ~ redis-cli
127.0.0.1:6379> JSON.SET doc . '{"name": "jamlee", "age": 18}'
OK

//Get the field in json
127.0.0.1:6379> JSON.GET doc .name
"\"jamlee\""
127.0.0.1:6379> JSON.GET doc .age
"18"

//Age add 1
127.0.0.1:6379> JSON.NUMINCRBY doc .age 1
"19"

//I want to insert a field attribute in json:
127.0.0.1:6379> JSON.SET doc .array '[1,2,3]'
OK
127.0.0.1:6379> JSON.GET doc
"{\"name\":\"jamlee\",\"age\":19,\"array\":[1,2,3]}"

//Add three elements to the array array in json
127.0.0.1:6379> JSON.ARRAPPEND doc .array true null false
(integer) 6

//Pop an element from the json array
127.0.0.1:6379> JSON.ARRPOP doc .array
"false"

//View json array
127.0.0.1:6379> JSON.GET doc .array
"[1,2,3,true,null]"

//View type
127.0.0.1:6379> JSON.TYPE doc
"object"

Let's show you a more excellent one:

//Create json, but it is a list
127.0.0.1:6379> JSON.SET lst . '[ true, { "answer": 42 }, null ]'
OK
//Get the answer attribute of the second element
127.0.0.1:6379> JSON.GET lst [1].answer
"42"

At this point, I will not enumerate them one by one. The operations supported are very numerous and flexible, just say whether it smells good to store json. I think it smells too good.

Then you say you started it with docker. What if I want to use it but don't want to start it with docker?

1.First download the pre compiled version from the Redis Download Center

https://redis.com/download-center/modules/

2.Next, run Redis using RedisJSON

$redis-server-- loadmodule / path/to/module/rejson.so

3.Summary

Now that Redis supports json, will you still use MongoDB? Through the above demonstration, I believe you can immediately see the difference between RedisJson and Hash, sometimes hash can't be done, RedisJson can be done very easily.

Label :
    Sign in for comments!
Comment list (0)

Powered by TorCMS (https://github.com/bukun/TorCMS).