This is the second day of my participation in the August More text Challenge. For details, see:August is more challenging

preface

Because of the premise of big data, Mysql has been unable to sustain the large data pressure. Do you want to move to Orcle or build Mysql clusters or join the new MongoDB?

The source of the mongo

Mongodb is not named after a mango. It comes from the Word “Mongo” in Humongous, which means “huge”

MongoDB is a non-relational database, as you’ve heard before. Other non-relational databases include Redis, ES.. But MongoDB is different. MongoDB is more like a relational database, which is Mysql. Why do you say that? Before we explain this, let’s take a look at how relational databases store data.

Relational database? No, a non-relational database

MySql’s storage structure is database -> tables -> records -> fields, while MongoDB’s storage structure is database -> Collections -> Documents -> Key and value pairs. Database corresponds to database, table corresponds to collection, record corresponds to document, field corresponds to Key, value. So that’s a quick way to understand the structure.

If you still don’t know the words, see the following example to help understand MongoDB’s storage example:

Title: TITLE_OF_POST, description: POST_DESCRIPTION, by: TITLE_OF_POST, title: TITLE_OF_POST, description: POST_DESCRIPTION, by: POST_BY, url: URL_OF_POST, tags: [TAG1, TAG2, TAG3], likes: TOTAL_LIKES, comments: [ { user:'COMMENT_BY', message: TEXT, dateCreated: DATE_TIME, like: LIKES }, { user:'COMMENT_BY', message: TEXT, dateCreated: DATE_TIME, like: LIKES } ] }Copy the code

In this example, you can see that it’s a Json format, but it’s not Json. It’s a Bson format. Bson is a jSON-like Binary format, which supports embedded document objects and array objects. But BSON has some data types that JSON does not, such as Date and BinData types.

SpringBoot use mongo

SpringBoot is very easy to use with MongoDB, and provides its own support to MongoDB, as well as the old way of importing dependencies. I didn’t even make a hammer

<! -- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mongodb --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> The < version > 2.5.3 < / version > < / dependency >Copy the code

Add the configuration in application.yml

` spring data mongo uri = mongo: / / user: PWD @ ip1: port1, ip2: port2 / database ` / / / / back to fill in the user name password, this depends on whether your mongo have open safety certificationCopy the code

Entity class

@Data
@Document(collection = "wfpe_data_broken_detection")
public class BrokenDetection{

    @Id
    @GeneratedValue
    private Long id;

    private String name;

    private Date addTime;
    
    private String sign;

    private Integer switcher;

    private Float data;

    private Long concentratorId;

    @Transient
    private String alarmInfo;


}
Copy the code

The use of MongoTemplate

Attention! The entity class must be annotated with @document so that the Mongodb database can be automatically generated to correspond to it

Insert the mongo

BrokenDetection data = new BrokenDetection(); data.set(...) Set field name... Save (data) mongodb mongotemplate-save (data);Copy the code

Query mongo

Query Query = new Query (Criteria. The where (BrokenDetection. SIGN) is (SIGN) / / is is = and (BrokenDetection. CONCENTRATORID) Is (concentratorId).and(brokenbroken.addtime).gte(startTime) // lte(endTime) // lte(endTime) // lte(endTime); Class List<BrokenDetection> brokenDetections = mongoTemplate. Find (query, brokendetection.class); // Pass in the query condition, pass in the class template;Copy the code

What I’m implementing here is a query that filters historical data by date.

conclusion

  1. MongoDB is a non-relational database, but it looks like a relational database

  2. MongDB using mongoTemplate can increase work efficiency

  3. MongoDB multiple conditions also require a conditional constructor