Talk about RedisJson’s blockbuster features


[gongzongid: Stack Future]

Redis supports JSON document storage, so you can give up mongodb?


1. Introduce mongo

MongoDB is a database based on distributed file storage. Written in C++ language, designed to provide scalable high-performance NoSQL data storage solutions for WEB applications.

MongoDB is a product between relational database and non-relational database. Among non-relational databases, it has the most rich functions and is the most like relational database.

MongoDB stores data as a document. The data structure consists of key=>value pairs.MongoDB documents are similar to JSON objects. Field values can contain other documents, arrays, and document arrays.

In the case of high load, MongoDB can add more nodes to ensure server performance.

Since MongoDB stores jSON-like documents, updating, deleting, or retrieving documents are all jSON-like operations. Take a look at MongoDB inserts. Here’s an example from the network:

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

Query:

> db.col.find({"by":"baidu"}).pretty()
{
        "_id" : ObjectId("56063f17ade2f21f36b03133"),
        "description" : "MongoDB是一个Nosql数据库",
        "by" : "baidu",
        "tags" : [
                "mongodb",
                "database",
                "NoSQL"
        ]
}

Copy the code

The jSON-like document actually has the same data structure as JSON, with all the data stored in the collection in BSON format.

BSON is a jSON-like storage format, short for Binary JSON.

2. RedisJson

RedisJSON – The JSON data type of Redis

Redis supports JSON storage and retrieval. For people like me, there are many advantages to storing JSON data. However, you may object that Redis already has a hash data type, so having a json type is a bit of a drag. After all, for many years, people have no JSON type, so what problem does it solve?

Don’t worry, please look down.

RedisJSON is a Redis module that implements the ECMA-404 JSON data interchange standard as a native data type. It allows you to store, update, and retrieve JSON values (documents) based on the Redis key.

Main features:

  • Fully supports the JSON standard

  • Select elements in the document using jsonPath-like syntax

  • Documents are stored as binary data in a tree structure with quick access to child elements

  • Atomic operations are defined for all JSON value types

RedisJSON was developed in Redis with <3. The source code is available at github.com/RedisJSON/R…

What does <3: mean? Ha, ha, ha, ha, ha, ha, ha, ha, ha, ha, ha, ha, ha, ha, ha.

Let’s give you a demonstration:

  1. Use Docker to launch RedisJSON

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

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

Copy the code

The following logs are displayed:

  1. Using RedisJSON

    Redis-cli can be accessed directly:

➜ ~ redis-cli 127.0.0.1:6379> json. SET doc. '{"name": "jamlee", "age": GET doc. name "\"jamlee\"" 127.0.0.1:6379> json.get doc. age "18" NUMINCRBY doc. age 1 "19" // I want to insert a field property in JSON: Array '[1,2,3]' OK 127.0.0.1:6379> json.get doc "{\" name \ ": \" jamlee \ ", \ "age \" : 19, \ "array \" : [1, 2, 3]} "/ / adds three elements to an array of json array 127.0.0.1:6379 > json. ARRAPPEND doc. The array True null false (integer) 6 // Pop an element out of the JSON array 127.0.0.1:6379> json.arrPOP doc. array "false" // View the JSON array Array "[1,2,3,true,null]" // check for type 127.0.0.1:6379> json. type doc "object"Copy the code

I’ll show you one more:

List 127.0.0.1:6379> json. SET LST. '[true, {"answer": 42}, null]' OK // Obtain the second element of the answer attribute 127.0.0.1:6379> json.get LST [1]. Answer "42"Copy the code

I’m not going to list all of them here, but there are a lot of different and flexible operations to support. If you want to use it to store JSON, I think it smells too good.

So people might say you’re docker booted, what if I want to use it but I don’t want to use Docker booted? Easy to say, dry goods:

  1. Download the pre-compiled version from the Redis Download center redis.com/download-ce…

  2. Next, run Redis using RedisJSON

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

Copy the code

3. Summary

Now that Redis supports JSON, will you still use MongoDB? I’m sure you can see that from the above demonstration, and you must have the answer in your mind.

And I’m sure you’ll immediately see the difference between RedisJson and Hash, because sometimes RedisJson is pretty easy to do when you can’t Hash.

Reference: oss.redis.com/redisjson/#…

Public reed id: Stack Future

So that a lot of confused stage coder can find the light from here, stack creation, achievement in the contemporary, future benefits.