I went to see my high school classmates this week, and I was quite happy. The original intention was to write the implementation of MySQL database transactions or the series of articles on MySQL optimization, but I haven't figured out how to assemble this knowledge.
Simply change direction this week and write NOSQL. Considering that some students don't know much about JSON, here we first introduce the basic concepts of MongDB, then explain why MongDB is introduced, and then explain how to use it.
what? what
MongoDB is a document database designed for ease of application development and scaling 《MongDB Official Website》
MongDB is a document database designed for high scalability and high availability.
Note that MongDB is a document database, what is the difference between a 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 consisting of field and value pairs. MongoDB documents are similar to JSON objects. The field's value can include other documents, arrays, and arrays of documents.
About what is JSON? See my article: Silly JSON?
So a MongDB record is a document similar to a JSON object, and 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 diagram:
We mentioned above that the record of MongDB is a document similar to JSON. The reason why it is similar is because MongDB adopts BSON, BSON = Binary JSON, which is JSON in binary form, but at the same time, it expands JSON and has JSON Data types that are not available in , such as date type (Date type), binary data type (BinData type).
Why introduce MongDB?
So why introduce MongDB? Or what advantages does MongDB have over relational databases?
Ten years ago, when Dwight and I started what would become MongoDB, we definitely didn't expect it to be what it is today. We have only one belief: make developers more efficient. MongoDB was born out of our frustration with using relational databases in large and complex business deployments. We set out to build a database we wanted to use ourselves. This way, whenever a developer wants to write an application, they can focus on the application itself, instead of going around the database. A decade of MongoDB, a founder's review
So in some areas, MongDB can make developers more efficient.
There is a question on Zhihu: What are the advantages, disadvantages and applicable scenarios of NoSQL such as MongoDB compared with relational databases? Below is an answer from a MongDB developer, here I briefly excerpt his answer:
- The document (JSON) model is more similar and more natural to object-oriented data representation. Unlike table structures 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 relational database three-paradigm form. But in practice we often see one-to-many and one-to-one data. For example, a blog post's tag list is very intuitive to be part of the article, but it is not so natural to put the affiliation of the tag and the article in a separate table. SQL speech can be formalized very precisely. However, the data emphasized by the three paradigms does not have any redundancy, which is not the most concerned issue for today's programmers. The more important issue is their convenience and inconvenience.
Here we come to the first advantage of MongDB over relational databases. In some scenarios, MongDB is more convenient than relational databases. Note that in some scenarios.
performance
The jojn brought by the three paradigms sometimes has to join multiple tables in order to satisfy a query, but the cost of joining multiple tables is that with the increase of data, the query speed will become very slow, and a simpler access mode can make It is 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 an index to obtain the optimal query speed.
flexible
If you need to add fields, you may need to change it from the database to the application layer. Although some tools can automate it, it is still a complex job. MongDB does not have a Schema, so there is no need to change the database, and only necessary changes need to be made at the application layer. (This sentence can be understood in this way. Look carefully at the diagram of MongDB accessing data drawn above. The students in 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 characteristics of LCD displays and LED displays are different. This characteristic of data is called polymorphism. In a relational database, how should the product table be designed? Generally speaking, the simplest solution is to turn each possible attribute into a separate column, but if the display manufacturer introduces a new feature every year, then this table will be changed frequently, and the scalability is not strong. A more extreme scenario is that an address book allows users to add new contact information at will. One solution is to put this custom contact information in a column and store it in json.
The flexibility of MongDB is also reflected in unstructured and semi-structured data. MongDB provides full-text indexing, and also supports geographic location queries and indexes. For example, a user wants to know where there are public toilets within a radius of five kilometers. This is a "geographical range query". Then he searches for the nearest bike. Mobike uses MongoDB to complete such a "distance sorting query". It is highly recommended to use a geo index to speed up queries, as I can understand this user's mood.
Scalability
MongDB comes with sharding natively, which corresponds to the sharding and sharding of relational databases.
- In which scenarios MongDB is not suitable
Zhou Siyuan, the developer of MongDB, cited a scenario where he needed a subway train timetable in a project. The official data provided is a SQL format that meets industry standards, that is, it is divided into several tables according to the three paradigms. Then he spent the night importing the database into MongDB. But he said that if he did it again, he might choose a relational database directly, because if the source data format is SQL data, it cannot be controlled, and the data volume is small; the cross-reference relationship is rich, the query mode is rich, and the application does not need high performance, then 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 databases still has powerful advantages in statistical data, query, and analysis.
how to use?
With, what and why, we can start using it, that is, start installing and using it. Fortunately, MongDB has a Chinese operation manual maintained by Chinese people:
The introduction is quite detailed, there are also installation tutorials, and there are free trials:
So for me, I chose the interview and trial, click in, the development scenario I chose is to develop microservices, you can also choose to install locally, the installation tutorial in this manual is quite detailed, I will put it here address, and will not be described in detail. https://docs.mongoing.com/install-mongodb
But for this kind of database in the cloud, it may be a little more troublesome to install the MongDB shell. I have been installing it on Windows for a long time and it seems that it has not been successful. Here, the GUI of MongDB can be used. The download address is as follows:
https://downloads.mongodb.com/compass/mongodb-compass-1.30.1-win32-x64.exe
Just fill in JSON
Java Manipulate MongDB
Now let's try to use it. I talked about some basic features in the first encounter, such as CRUD. First, the connection to the database must be driven by:
<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 with the database are generally to obtain a connection and then send a statement. In the Java driver of MongDB, the addition, deletion, modification and query are related to the two classes of Document and Bson:
When we add an object, it is a new Document, like losing value in it. Filter is used to construct a condition object for querying:
Filters.ne 是不等于
Filters.gt 大于
Filters.and(Filters.eq("age",2222)),Filters.eq("name","aaa") 连等于
Filters.gte 大于等于
Filters.lt 小于
Filters.lte 小于等于
Filters.in()
Filters.nin() not in
Filters are essentially to help you pass operators, we can also achieve the same effect with the help of Document:
new Document("name","张三") = Filters.eq("name","张三")
new Document("age",new Document("$eq",24)) = Filters.eq("name","张三")
new Document("age",new Document("$ne",24)) = Filters.ne("name","张三")
We mainly rely on MongoCollection to complete the addition, deletion and modification in MongDB, which is mainly related to Document. Modification and deletion can be implemented using Filters and Documents.
- 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");
// study 这张表的存的有 number name id, 如果是关系型数据库,我们要添加属性,需要先在表中加字段,
// 现在 在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 是等于运算符,Filters.eq("age",2222) 表示查出定位的是age = 2222的对象
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 可以是一个数组, 所有的值都得是2222
Bson condition = Filters.all("age", 2222);
FindIterable<Document> documentList = studyTable.find(condition);
}
Finally to sum up
To a certain extent, we take data from real data, process it and send it to the database, and then retrieve the data from the database according to the user's request, but the data structure in the real world is all kinds, and the relational database is also unable to catch it. Scenarios such as why we introduced MongDB as discussed above, and another scenario is to describe the connection between data, such as organizational structure, interpersonal relationship diagram, when the amount of data is relatively large, relational databases are faced with The problem of slow query speed, in order to describe this nonlinear relationship, the graph database came into being. Different databases are dealing with different data description scenarios.
References
- MongoDB Chinese Manual | Official Document Chinese Version https://docs.mongoing.com/
- Compared with relational databases, what are the advantages, disadvantages and applicable scenarios of NoSQL such as MongoDB? https://www.zhihu.com/question/20059632
- BSON official website https://bsonspec.org/#/
- java operation mongodb - query data https://www.cnblogs.com/simple-ly/p/5796440.html
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。