106 MongoDB related concepts

MongoDB related concepts

      • 1.1 Business application scenarios
      • 1.2 Introduction to MongoDB
      • 1.3 Architecture
      • 1.4 Data model
      • 1.5 Features of MongoDB
    • 2 docker install mongoDB
    • 3 Connect to mongodb
    • 4 Create database and collection
      • 4.1 Create database
      • 4.2 Create a collection
    • 5 springboot basic mongdb
      • 5.1 Build the project
        • 5.1.1 Create project
        • 5.2.2 Integrate mongodb
      • 5.2 crud of mongodb
        • 5.2.1 Entity class preparation
        • 5.2.2 Basic addition, deletion, modification and query

1 MongoDB related concepts

1.1 Business application scenarios

Traditional relational databases (such as MySQL) are unable to cope with the “three high” requirements of data operations and the website requirements of Web2.0.

Explanation: “Three High” requirements:

? High performance – requirements for high concurrent reading and writing of the database.

? Huge Storage – The need for efficient storage and access of massive data.

? High Scalability & amp; & amp; High Availability- The need for high scalability and high availability of the database.

MongoDB can cope with the “three high” demands.

Specific application scenarios include:

1) In social scenarios, MongoDB is used to store user information and the user’s circle of friends information, and functions such as nearby people and places are implemented through geographical location indexing.

2) In the game scene, MongoDB is used to store game user information. The user’s equipment, points, etc. are directly stored in the form of embedded documents, which facilitates query, efficient storage and access.

3) In the logistics scenario, MongoDB is used to store order information. The order status will be continuously updated during the delivery process and is stored in the form of an embedded array in MongoDB. All changes to the order can be read in one query.

4) In the Internet of Things scenario, MongoDB is used to store the information of all connected smart devices and the log information reported by the devices, and perform multi-dimensional analysis on this information.

5) Live video broadcast, use MongoDB to store user information, like interaction information, etc.

The common characteristics of data operations in these application scenarios are:

(1) Large amount of data

(2) Frequent writing operations (both reading and writing are frequent)

(3) Data with low value and low transaction requirements

For such data, we are more suitable to use MongoDB to store the data.

Behavior

When to choose MongoDB

In terms of architecture selection, in addition to the above three characteristics, if you are still hesitant whether to choose it? Here are some questions to consider:

The application does not require transaction or complex join support

For new applications, the requirements will change and the data model cannot be determined. We want to develop iteratively quickly.

The application requires more than 2000-3000 read and write QPS (higher is also acceptable)

Applications require terabytes or even petabytes of data storage

Applications develop rapidly and require rapid horizontal expansion

The application requires that stored data is not lost

Applications require 99.999% high availability

The application requires a large number of geographical location queries and text queries

If one of the above applies, you can consider MongoDB. If two or more apply, you will never regret choosing MongoDB.

Thinking: What if you use MySQL?

Answer: Compared with MySQL, the problem can be solved at a lower cost (including learning, development, operation and maintenance, etc. costs)

1.2 Introduction to MongoDB

MongoDB is an open source, high-performance, schema-less document database. It was originally designed to simplify development and facilitate expansion. It is one of the NoSQL database products. It is a non-relational database that is most similar to a relational database (MySQL).

The data structure it supports is very loose, a format similar to JSON called BSON, so it can store more complex data types and is quite flexible.

A record in MongoDB is a document, which is a data structure composed of field and value pairs (field:value). MongoDB documents are similar to JSON objects, that is, a document is considered an object. The data type of the field is character type. In addition to using some basic types, its value can also include other documents, ordinary arrays and document arrays.

1.3 Architecture

Comparison between MySQL and MongoDB

SQL terms/concepts MongoDB terms/concepts Explanation/Explanation
database database Database
table collection Database table/collection
row document Data record row/document
column field Data field/domain
index index index
table joins Table joins, MongoDB does not support
Embedded document MongoDB replaces it with embedded document Multi-table join
primary key primary key Primary key, MongoDB automatically sets the _id field as the primary key

1.4 Data Model

The smallest storage unit of MongoDB is the document object. Document objects correspond to rows in a relational database. Data is stored on disk in MongoDB in the format of BSON (Binary-JSON) documents.

BSON (Binary Serialized Document Format) is a binary storage format similar to json, referred to as Binary JSON. Like JSON, BSON supports embedded document objects and array objects, but BSON has some data types that JSON does not have, such as Date and BinData types.

BSON adopts a name and pair representation method similar to C language structures, supports embedded document objects and array objects, has the three characteristics of lightweight, traversability, and efficiency, and can effectively describe unstructured data and Structured data. The advantage of this format is high flexibility, but its disadvantage is that space utilization is not very ideal.

In Bson, in addition to the basic JSON types: string, integer, boolean, double, null, array and object, mongo also uses special data types. These types include date, object id, binary data, regular expression and code. Each driver implements these types in a language-specific way, see your driver’s documentation for details.

BSON data type reference list:

Data type Description Example
Object id The object id is the 12-byte unique ID of the document { “X” :ObjectId() }
String UTF-8 strings can be represented as string type data {“x” : “foobar”}
Boolean value True or false: true or false {” x”:true} +
Array A collection or list of values can be represented as an array {“x”: [ “a”, “b”, “c”]}
The 32-bit integer type is not available. JavaScript only supports 64-bit floating point numbers, so 32-bit integers are automatically converted. The shell does not support this type. The shell will convert it to a 64-bit floating point number by default
64-bit integer No This type is supported. The shell will use a special embedded document to display 64-bit integers The shell does not support this type, and the shell will convert it to a 64-bit floating point number by default
64-bit floating point numbers The numbers in the shell are of this type {“x”: 3.14159, “y”: 3}
null Represents a null value or an undefined object {“x”:null}
undefined Undefined types can also be used in documents {“x”:undefined}
Symbol The shell does not support it. The shell will automatically convert symbol type data in the database into strings
Regular expression Documents can contain regular expressions, using JavaScript regular expression syntax {“x”: /foobar/i}
code The document can also contain JavaScript code {“x”: function() { /* …… */ }}
Binary data Binary data can be composed of any byte string, but it cannot be used in the shell
Maximum value /minimum BSON includes a special type that represents the maximum possible value. There is no such type in the shell.

hint:

The shell uses 64-bit floating point values by default. {“x”: 3.14} or {“x”: 3}. For integer values, you can use NumberInt (4-byte signed integer) or NumberLong (8-byte signed integer), {“x”:NumberInt(“3”)}{“x”:NumberLong(“3”)}

1.5 Features of MongoDB

MongoDB mainly has the following features:

(1)High performance:

MongoDB provides high-performance data persistence. in particular,

Support for embedded data models reduces I/O activity on the database system.

Indexes support faster queries and can contain keys from embedded documents and arrays. (Text index solves the need for search, TTL index solves the need for automatic expiration of historical data, and geographical location index can be used to build various O2O applications)

Multiple engines such as mmapv1, wiredtiger (default), mongorocks (rocksdb), and in-memory are supported to meet the needs of various scenarios.

Gridfs solves file storage needs.

(2)High availability:

MongoDB’s replication tool, called a replica set, provides automatic failover and data redundancy.

(3)High scalability:

MongoDB provides horizontal scalability as part of its core functionality.

Sharding distributes data across a cluster of machines. (Massive data storage, horizontal expansion of service capabilities)

Starting from 3.4, MongoDB supports creating data regions based on shard keys. In a balanced cluster, MongoDB directs reads and writes covered by a region only to those shards within that region.

(4)Rich query support:

MongoDB supports a rich query language and supports read and write operations (CRUD), such as data aggregation, text search and geospatial query.

(5) Other features: such as modeless (dynamic mode), flexible document model,

2 docker installation mongoDB

2.1 Pull the image

docker pull mongo:4.2.5

2.2 Create container

docker run --name mongo --restart always -p 27017:27017 -v ~/data/mongodata:/data -d mongo:4.2.5

3 Connect mongodb

studio3t is an excellent client tool for mongodb. The official address is https://studio3t.com/

Or use: https://www.mongodb.com/try/download/compass

download studio3t
Install and start
Create connection

4 Create database and collection

4.1 Create database

Create a database using studio3t

Right click->Add Database

4.2 Create collection

Right click leadnews-comment -> Add Collection

5 springboot basic mongdb

5.1 Building the project

5.1.1 Create project

Create project: mongodb-test

pom file:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
</dependencies>

application.yml

server:
  port: 9001
spring:
  application:
    name: mongo-demo
  data:
    mongodb:
      host: 192.168.200.130
      database: leadnews-comment

Boot class:

package com.it.mongo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MongoApplication {<!-- -->
    public static void main(String[] args) {<!-- -->
        SpringApplication.run(MongoApplication.class,args);
    }
}
5.2.2 Integrating mongodb

5.2 crud of mongodb

5.2.1 Entity class preparation

Entity class preparation, add annotation @Document to correspond to the collection in mongodb

package com.it.mongo.pojo;

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.math.BigDecimal;
import java.util.Date;
@Data
@Document(collection = "ap_comment")
public class ApComment {<!-- -->
    @Id
    private String id;
    private Integer authorId;
    private String authorName;
    private Integer entryId;
    private Integer channelId;
    private Boolean type;
    private String content;
    private String image;
    private Integer likes;
    private Integer reply;
    private Byte flag;
    private BigDecimal longitude;
    private BigDecimal latitude;
    private String address;
    private Integer ord;
    private Date createdTime;
    private Date updatedTime;

}
5.2.2 Basic addition, deletion, modification and query

Create test class:

package com.it.mongo;

import com.it.mongo.pojo.ApComment;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Version: V1.0
 */
@SpringBootTest
class ApCommentTest {<!-- -->
  @Autowired
  MongoTemplate mongoTemplate;
  @Test
  public void testAdd() throws Exception{<!-- -->
    for (int i = 0; i < 10; i + + ) {<!-- -->
      ApComment apComment = new ApComment();
      // apComment.setId("2222");
      apComment.setAddress("Shanghai");
      apComment.setContent("Shanghai is a good place222" + i);
      apComment.setLikes(2 + i);
      // 60c2fe86c5a9e6651633a54a ObjectId
      // ApComment comment = mongoTemplate.insert(apComment);
      ApComment comment = mongoTemplate.save(apComment);
      System.out.println(comment);
    }
  }
  @Test
  public void testUpdate() throws Exception {<!-- -->
    Query query = Query.query(Criteria.where("address").is("Shanghai"));
    Update update = new Update();
    update.set("address", "Beijing").set("createdTime", new Date()); // set modifier: only the columns you specify will be modified
    // UpdateResult updateResult = mongoTemplate.updateFirst(query, update, ApComment.class);
    // System.out.println(updateResult);
    UpdateResult updateResult = mongoTemplate.updateMulti(query, update, ApComment.class);
    System.out.println(updateResult);
  }
  @Test
  public void testDelete() throws Exception {<!-- -->
    Query query = Query.query(Criteria.where("_id").is("60c2ffe79722b513119702b3"));
    DeleteResult deleteResult = mongoTemplate.remove(query, ApComment.class);
    System.out.println(deleteResult);
  }
  // Query a
  @Test
  public void testFindOne() throws Exception {<!-- -->
    // ApComment comment = mongoTemplate.findById("60c2fe86c5a9e6651633a54a", ApComment.class);
    Query query = Query.query(Criteria.where("address").is("Beijing"));
    ApComment comment = mongoTemplate.findOne(query, ApComment.class);
    System.out.println(comment);
  }
  // Query all
  @Test
  public void testFindAll() throws Exception {<!-- -->
    List<ApComment> commentList = mongoTemplate.findAll(ApComment.class);
    for (ApComment apComment : commentList) {<!-- -->
      System.out.println(apComment);
    }
  }
  // Query according to conditions
  @Test
  public void testFindByQuery() throws Exception{<!-- -->
    Query query = Query.query(Criteria
                              .where("address").is("Beijing")
                              .and("_id").in("60c301de64ffe574631f0c44","60c301de64ffe574631f0c47")
                             );

    List<ApComment> apComments = mongoTemplate.find(query, ApComment.class);
    System.out.println(apComments);
  }
  // Paging query
  @Test
  public void testFindPage() throws Exception {<!-- -->

    Query query = Query.query(Criteria
                              .where("address").is("Beijing")
                             );
    // paging
    int page = 0;
    int size = 5;
    // int limit = (page - 1) * size;
    // query.limit(5); // size

    Pageable pageable = PageRequest.of(1, 5); // page=0 first page
    query.with(pageable); // paging

    Sort sort = Sort.by(Sort.Direction.DESC,"likes");
    query.with(sort);// Sort

    List<ApComment> commentList = mongoTemplate.find(query, ApComment.class);
    System.out.println(commentList);
  }

}

Data can be viewed in studio3t