mongodb is one of the earliest popular non-relational databases and is also commonly used. It is generally used for offline data analysis, and most of them are placed on the intranet. Since many companies use cloud services, the servers have opened external network addresses by default, resulting in a large number of MongoDBs being attacked due to configuration vulnerabilities a while ago and the data was deleted, which attracted people's attention. Those who are interested can read this article: The Feast of MongoDB slaughtering in the field Reflection: More than 33,000 databases were invaded and ransomware, which also shows that many companies use mongodb in production.
Introduction to mongodb
MongoDB (from the English word "Humongous" and Chinese meaning is "Humongous") is an open source database that can be applied to enterprises of all sizes, industries and applications of all kinds. A database based on distributed file storage. Written in C++. Designed to provide scalable, high-performance data storage solutions for WEB applications. MongoDB is a high-performance, open source, schema-free document-based database, and is a popular one among the current NoSql databases.
MongoDB is a product between a relational database and a non-relational database. It is the most functional and most similar to a relational database among non-relational databases. The data structure it supports is very loose and is in a json-like bjson format, so it can store relatively complex data types. Mongo's biggest feature is that the query language it supports is very powerful. Its syntax is a bit similar to an object-oriented query language. It can almost realize most of the functions similar to single table query of relational databases, and it also supports indexing of data.
Traditional relational databases generally consist of three levels of concepts: database, table, and record. MongoDB is composed of three levels of database, collection, and document object. MongoDB has tables in relational databases, but there are no concepts of columns, rows and relationships in the set, which reflects the characteristics of pattern freedom.
A record in MongoDB is a document, a data structure, composed of field and value pairs. MongoDB documents are similar to JSON objects. The value of the field may include other documents, arrays, and document arrays. MongoDB supports operating systems such as OS X, Linux and Windows, and provides drivers for Python, PHP, Ruby, Java and C++ languages. The community also provides drivers for platforms such as Erlang and .NET.
MySQL is suitable for storing large amounts of data without fixed formats, such as logs, caches, etc. Weak support for things and does not apply to complex multi-document (multiple table) cascading queries. The mongodb version is 3.4 in the article.
Recently, I took over a Springboot project and needed to add some demand to the original project, using mongodb. Let’s take a look at the integration path!
1. First, introduce the mongodbDe dependency jar package in pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
2. Create entity class
@Document(collection = "spiderConfig")public class SpiderConfig implements Serializable { @Id private String id; private String spiderConfig; private long updateTime; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getSpiderConfig() { return spiderConfig; } public void setSpiderConfig(String spiderConfig) { this.spiderConfig = spiderConfig; } public long getUpdateTime() { return updateTime; } public void setUpdateTime(long updateTime) { this.updateTime = updateTime; }}3. The code for operating mongodb on the dao layer
@Componentpublic class SpiderConfigDaoImpl implements ISpiderConfigDao{ @Autowired private MongoTemplate mongoTemplate; @Override public SpiderConfig findById(String id) { Query query=new Query(Criteria.where("_id").is(id)); SpiderConfig user = mongoTemplate.findOne(query , SpiderConfig.class); return user; } @Override public void saveSpiderConfig(SpiderConfig spiderConfig) { mongoTemplate.save(spiderConfig); }}4. Set up the relevant configuration information of the mongodb database in application.properties
#mongodb configuration spring.data.mongodb.uri=mongodb://192.168.86.888:27017/test
The configuration of relevant information has been completed, and the interaction between the test and verification and the database is correct!
Summarize
The above is the operation method of Sprint Boot integrated MongoDB introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!