Talk about redisjason's blockbuster features
[gongzong No.: future]
dried food:
redis supports json document storage. Can you give up mongodb?
1. Introduction to mongodb
MongoDB is a database based on distributed file storage. Written in C + + language, it aims to provide scalable high-performance data NoSQL storage solution for WEB applications.
MongoDB is a product between relational database and non relational database. It is the most functional and relational database among non relational databases.
MongoDB stores data as a document, and the data structure consists of key value (key = > value) pairs. A MongoDB document is similar to a JSON object. Field values can contain other documents, arrays, and document arrays.
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 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 It's a Nosql database', by: 'baidu', tags: ['mongodb', 'database', 'NoSQL'], })
Query:
> db.col.find({"by":"baidu"}).pretty() { "_id" : ObjectId("56063f17ade2f21f36b03133"), "description" : "MongoDB It's a Nosql database", "by" : "baidu", "tags" : [ "mongodb", "database", "NoSQL" ] }
In fact, the data structure of this class JSON document is basically the same as that of JSON. All the data stored in the collection is in BSON format.
BSON is a binary storage format similar to JSON, which is short for Binary JSON.
2. RedisJson
Redisjson - the JSON data type of redis
You're right. It's a new feature of redis: it supports JSON storage and retrieval. I think this function is Wang fried. For people like me, storing JSON data has many advantages, but you may object. Redis already has hash data types. It is inevitable that one more JSON type will be a bit weak. After all, for so many years, everyone has come here without JSON type. So what problem does it solve?
Don't worry, please look down.
RedisJSON is a Redis module that implements ECMA-404 JSON data exchange standard as a local data type. It allows you to store, update and obtain JSON values (documents) according to the Redis key.
Main features:
- Fully support JSON standard
- Select elements in the document using JSONPath like syntax
- The document is stored as binary data in a tree structure, which can quickly access child elements
- Atomic operations are defined for all JSON value types
Redisjson is developed in Redis with < 3. The source code can be downloaded from https://github.com/RedisJSON/RedisJSON get.
❤️: What does that mean? Ha ha, those who know this are very powerful. If you don't know, go down and check it.
Next, let's show you:
- Start RedisJSON using Docker
Use Docker to run the following commands on Windows, MacOS, or Linux:
docker run -p 6379:6379 --name redis-redisjson redislabs/rejson:latest
Print the log as follows:
- Using RedisJSON
With redis cli, you can directly enter:
//Insert json data. The key is doc and the 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" //Add 1 to my age 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 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 another powerful one:
//Create json, but there is a list inside 127.0.0.1:6379> JSON.SET lst . '[ true, { "answer": 42 }, null ]' OK //Gets the answer attribute of the second element 127.0.0.1:6379> JSON.GET lst [1].answer "42"
I won't list them here one by one. The supported operations are very many and flexible. You say whether it is fragrant to store json. I think it's too fragrant.
Then you said you started it with docker. What if I want to use it but don't want to start it with docker? Well, dry goods:
- First download the precompiled version from the Redis Download Center
https://redis.com/download-center/modules/ - Next, run Redis using RedisJSON
$ redis-server --loadmodule /path/to/module/rejson.so
3. Summary
Since redis supports json, will you still use MongoDB?
Through the above demonstration, I believe you can immediately see the difference between redisjason and hash. Sometimes hash can't do it. Redisjason can do it very easily.
[gongzong No.: future]
reference resources: https://oss.redis.com/redisjson/#redis-cloud