MongDB learning notes first encounter

I went to see my high school classmates this week. I'm still very happy. I originally intended to write articles on the implementation of MySQL database transactions or MySQL optimization series, but I haven't thought about how to assemble these knowledge.

Simply change direction this week and write NOSQL. Considering that some students don't know much about JSON, let's first introduce the basic concept of MongDB, then explain why MongDB should be introduced, and then explain how to use it.

What is it? what

MongoDB is a document database designed for ease of application development and scaling

MongDB is a document database designed for high scalability and high availability.

Note that MongDB is a document database. What is the difference between document database and our common relational database? Let's first look at this document, that is, what does document mean?

A record in MongoDB is a document, which is a data structure composed of fields and value pairs. A MongoDB document is similar to a JSON object. Field values can include other documents, arrays, and document arrays.

About what is JSON? See my article: silly JSON?

Therefore, the record of MongDB is a document similar to a JSON object. MongDB stores the document in a collection, which is similar to a table in a relational database. The access relationship is similar to the following figure:

We mentioned above that the record of MongDB is a document similar to JSON. The reason why it is similar to is that MongDB adopts BSON. BSON = Binary JSON, that is, JSON in binary form. At the same time, it expands JSON and has data types that are not available in JSON, such as date type and bindata type.

Why introduce MongDB?

Then why introduce MongDB? Or what are the advantages of MongDB over relational databases?

Ten years ago, when Dwight and I started this project, which later became MongoDB, we absolutely didn't expect it to look like today. We have only one belief: make developers more efficient. MongoDB was born out of the frustration of using relational databases in large and complex business deployment. We set out to build a database we wanted to use. In this way, whenever developers want to write an application, they can focus on the application itself rather than around the database. Ten years of MongoDB, a review of the founder

So in some areas, MongDB can make developers more efficient.

I know there is a question: what are the advantages, disadvantages and applicable scenarios of NoSQL such as MongoDB compared with relational databases? One of the developers of MongDB answered the following questions. Here I will briefly extract his answer:

  • The object-oriented model is more similar to the JSON data model. Unlike the table structure in relational databases, arrays and subdocuments (JSON) can be embedded in documents (JSON), just like arrays and member variables in programs. This is not allowed in the three paradigms of relational database. But in practice, we often see one-to-many and one-to-one data. For example, the Tag list of a blog post is very intuitive as a part of the article, but it is not so natural to put the dependency between Tag and the article in a separate table. SQL voice can be expressed accurately. However, the three paradigms emphasize that there is no redundancy in the data, which is not the most concerned problem of programmers today. The more important problem is whether they are convenient to use.

Here we come to the first advantage of MongDB over relational database. In some scenarios, MongDB is more convenient than relational database. Note that in some scenarios.

  • performance

    jojn brought by the three paradigms sometimes has to join multiple tables in order to meet a query, but the cost of joining multiple tables is that with the improvement of data, the query speed will become very slow. A simpler access mode can make it easier for developers to understand the performance of the database. A typical scenario is that I join ten tables. In this case, How can I add index to get the best query speed.

  • flexible

    If you need to add fields, you may need to change them from the database to the application layer. Although some tools can automate it, it is still a complex work. Without Schema, MongDB does not need to change the database, but only needs to make necessary changes in the application layer. (this sentence can be understood as follows. Take a closer look at the above diagram of MongDB accessing data. The students accessing the collection now have three fields. In fact, MongDB allows documents to have different fields)

Custom attributes are also a problem. For example, the product features of LCD display and LED display are different. This feature of data is called polymorphism. How should the product table be designed in a relational database? Generally speaking, the simplest solution is to turn each possible attribute into a separate column, but if the display manufacturer launches a new feature every year, the table will have to be changed frequently and the scalability is not strong. Another more extreme scenario is that an address book allows users to add new contact information at will. One scheme is to put this custom contact information in one column and store it in json.

The flexibility of MongDB is also reflected in unstructured and semi-structured data. MongDB provides full-text index and supports geographic location query and index. For example, a user wants to know where there is a public toilet within a five kilometer radius. This is "geographical range query". Then he searched the nearest bike. Moby bike uses MongoDB to complete such "distance Sorting Query". It is strongly recommended to use geographic location index to speed up query, because I can understand the user's mood.

  • Scalability

    MongDB has its own partition, which corresponds to the database and table of relational database.

  • Which scenarios are not suitable for MongDB

Zhou Siyuan, the developer of MongDB, gave a scenario. He needed a subway train timetable in a project. The official data provided is a SQL format that meets the industry standard, that is, it is divided into several tables according to the three paradigms. Then he spent the whole night importing the database into MongDB. But he said that if he did it again, he might directly select the relational database, because if the source data format is SQL data, there is no control and the amount of data is small; With rich cross reference relationships, rich query patterns, and applications that do not require high performance, relational data pages are also a pragmatic choice.

My understanding of this sentence is that from the perspective of modeling application data, in some scenarios, SQL provided by relational database still has strong advantages in statistics, query and analysis.

How to use it?

With what and why, we can start to use it, that is, to install and use it. Fortunately, MongDB has a Chinese operation manual maintained by Chinese people:

The introduction is quite detailed. There are also installation tutorials and free trials:

So for me, I chose to interview and try. Click in. The development scenario I chose is to develop micro services. You can also choose to install locally. The installation tutorial in this manual is still very detailed. I'll put an address here instead of making a detailed introduction. https://docs.mongoing.com/ins...

However, for this kind of database in the cloud, it may be a little troublesome to install the MongDB shell. I haven't succeeded in installing it on windows for a long time. Here you can use the MongDB GUI. The download address is as follows:

https://downloads.mongodb.com...

Just fill in JSON

Java manipulation MongDB

Now let's try to use it. What I talked about in my first encounter is some basic features, such as CRUD. First, the driver must be connected to the database:

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.12.5</version>
</dependency>

The basic steps of adding, deleting, modifying and querying in dealing with the database are generally to obtain the connection and then send the statement. In the java driver of MongDB, adding, deleting, modifying and querying are related to the two classes of Document and Bson:

When we add a new object, it is a new Document, like losing value in it. Filter is used to construct condition objects and query:

Filters.ne Is not equal to
Filters.gt greater than
Filters.and(Filters.eq("age",2222)),Filters.eq("name","aaa") Even equal
Filters.gte Greater than or equal to
Filters.lt less than
Filters.lte Less than or equal to
Filters.in()
Filters.nin() not in

In essence, Filters is to help you pass the operator. We can also achieve the same effect with the help of Document:

 new Document("name","Zhang San") = Filters.eq("name","Zhang San")
 new Document("age",new Document("$eq",24))  = Filters.eq("name","Zhang San")
 new Document("age",new Document("$ne",24))  = Filters.ne("name","Zhang San")    

We mainly use MongoCollection to complete the addition, deletion, query and modification of MongDB. The addition is mainly related to Document. Modification, deletion and query can be realized by using Filters and Document.

  • increase
@Test
void contextLoads() throws Exception {
    ConnectionString connectionString = new ConnectionString("mongodb+srv://study:a872455@cluster0.exlk2.mongodb.net/myFirstDatabase?retryWrites=true&w=majority");
    MongoClientSettings settings = MongoClientSettings.builder()
            .applyConnectionString(connectionString)
            .build();
    MongoClient mongoClient = MongoClients.create(settings);
    insertDocumentDemo(mongoClient);
}

public void insertDocumentDemo(MongoClient mongoClient) throws  Exception {
    MongoDatabase database = mongoClient.getDatabase("test");
    database.getCollection("study");
    MongoCollection<Document> study = database.getCollection("study");
    // number name id is stored in the study table. If it is a relational database, we need to add fields in the table before adding attributes,
    // Now we add it directly in MongDB
    Map<String,Object> map = new HashMap<>();
    map.put("number","bbb");
    map.put("name","aaa");
    map.put("id","1111");
    map.put("age",2222);
    Document document = new Document(map);
    study.insertOne(document);
}

result:

  • Delete
public void deleteDocument(MongoClient mongoClient){
        MongoDatabase database = mongoClient.getDatabase("test");
        database.getCollection("study");
        MongoCollection<Document> study = database.getCollection("study");
        // eq is the equals operator, filters eq ("age", 2222) indicates that the object with age = 2222 is detected and located
        study.deleteOne(Filters.eq("age",2222));
}
  • change

    public void updateDocument(MongoClient mongoClient){
            MongoDatabase database = mongoClient.getDatabase("test");
            database.getCollection("study");
            MongoCollection<Document> study = database.getCollection("study");
            study.updateOne(Filters.eq("age",2222),new Document("$set",new Document("name","22222")));
     }
  • check
public void selectDocument(MongoClient mongoClient){
        MongoDatabase dataBase = mongoClient.getDatabase("test");
        MongoCollection<Document> studyTable = dataBase.getCollection("study");
        // age can be an array, and all values must be 2222
        Bson condition = Filters.all("age", 2222);
        FindIterable<Document> documentList = studyTable.find(condition);
}

Finally, let's summarize

To some extent, we all take the data from the real data, send it to the database after processing, and then take out the data in the database according to the user's request. However, the data structures in the real world are diverse, and the relational database is powerful and can't catch the scene. For example, we discussed why we introduced MongDB above, Another scenario is to describe the relationship between data, such as organizational structure and interpersonal graph. When the amount of data is large, relational database is faced with the problem of slow query speed. In order to describe this nonlinear relationship, graph database came into being. Different databases are dealing with different data description scenarios.

reference material

Keywords: MongoDB

Added by plasko on Sun, 06 Mar 2022 09:25:59 +0200