No database

Introduction to NoSQL(Not Only SQL)

  1. Non-relational databases, such as MongoDB, support relatively complex data structures and can access associated data in the form of documents to reduce data association operations
  2. Dramatically provides storage performance through distributed storage of data
  3. Provides powerful data availability, in the application, can quickly feedback information to the user
  4. Transaction processing and consistency weak, if the performance requirements, only to achieve data consistency

A project can have both a NoSQL database and an RDB database, separating the data from the design

  • No classification

  • What is MongoDB and its features

  1. Website: docs.mongodb.com
  2. Written by C++ language, based on distributed file storage open source NoSQL database system
  3. The data is stored as a BSON document (BSON specification), and the data structure consists of key => value pairs. The field value can contain other documents, arrays and document data. MongoDB can be imagined as a super large object

Note: Field corresponds to Column in relational database, Collection corresponds to Table in relational database

4. Query function is very powerful, similar to object-oriented programming language

  • Operate MongoDB in Node.js

  • What is a Redis

Liverpoolfc.tv: redis. IO /

An open source, BSD-compliant, network-enabled, memory-based, distributed, optionally persistent key-value pair for database storage, written based on ANSI C, with multiple language apis

  • Redis installation (macOS)

In the case of macOS, other system reference: www.runoob.com/redis/redis…

  1. Install Homebrew (already installed ignore this step)

    /bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)"
    Copy the code
  2. Install redis

    brew install redis
    Copy the code
  3. If you start Redis, port 6379 will be used by default

        redis-server Redis-server --daemonize yes
    Copy the code

    Use the ps – ef | grep -i redis see if redis started

  4. Stopping redis and forcibly terminating the Redis process may cause data loss. Therefore, send the shutdown command to Redis

    redis-cli shutdown
    Copy the code

    Note: Redis can handle SIGTERM signals properly, so using the PID of the kill Redis process can also end Redis normally

  5. Redis persistent: The default Redis persistent mode is RDB, which can be changed to AOF through the conf configuration file appendonly yes

  • Redis graphics management software RDM

Rdm.dev /

  • Operate Redis in Node.js

IO /clients#nod… With ioredis (github.com/luin/ioredi…). For example (Ioredis is widely used. For example, Alibaba uses Ioredis in its B2B shopping mall and supports native Promise)

  1. The basic use

const Redis = require("ioredis");
const redis = new Redis(); // uses defaults unless given configuration object

// ioredis supports all Redis commands:
redis.set("foo"."bar"); // returns promise which resolves to string, "OK"

// the format is: redis[SOME_REDIS_COMMAND_IN_LOWERCASE](ARGUMENTS_ARE_JOINED_INTO_COMMAND_STRING)
// the js: ` redis.set("mykey", "Hello") ` is equivalent to the cli: ` redis> SET mykey "Hello" `

// ioredis supports the node.js callback style
redis.get("foo".function (err, result) {
  if (err) {
    console.error(err);
  } else {
    console.log(result); // Promise resolves to "bar"}});// Or ioredis returns a promise if the last argument isn't a function
redis.get("foo").then(function (result) {
  console.log(result); // Prints "bar"
});

// Most responses are strings, or arrays of strings
redis.zadd("sortedSet".1."one".2."dos".4."quatro".3."three");
redis.zrange("sortedSet".0.2."WITHSCORES").then((res) = > console.log(res)); // Promise resolves to ["one", "1", "dos", "2", "three", "3"] as if the command was ` redis> ZRANGE sortedSet 0 2 WITHSCORES `

// All arguments are passed directly to the redis server:
redis.set("key".100."EX".10);
Copy the code

2. The pipe (Pipelining)Redis. IO/switchable viewer/pipe…

const pipeline = redis.pipeline();
pipeline.set("foo"."bar");
pipeline.del("cc");
pipeline.exec((err, results) = > {
  // `err` is always null, and `results` is an array of responses
  // corresponding to the sequence of queued commands.
  // Each response follows the format `[err, result]`.
});

// You can even chain the commands:
redis
  .pipeline()
  .set("foo"."bar")
  .del("cc")
  .exec((err, results) = > {});

// `exec` also returns a Promise:
const promise = redis.pipeline().set("foo"."bar").get("foo").exec();
promise.then((result) = > {
  // result === [[null, 'OK'], [null, 'bar']]
});
Copy the code

3. The transaction

redis
  .multi()
  .set("foo"."bar")
  .get("foo")
  .exec((err, results) = > {
    // results === [[null, 'OK'], [null, 'bar']]
  });
Copy the code

A transaction has pipes by default. If no pipes are needed (not recommended, pipes improve efficiency), {pipeline: false} needs to be passed to multi and each command will be sent to Redis immediately without waiting for exec to call

4. Error message

ShowFriendlyErrorStack is enabled to optimize the error stack