MongoDB makes its debut
To introduce myself
MongoDB is a database based on distributed file storage. Written in C++ language. Designed to provide scalable high-performance data storage solutions for WEB applications.
MongoDB is a product between relational database and non-relational database. Among non-relational databases, it has the most rich functions and is the most like relational database.
The biggest characteristic of MongoDB is that it has no Schema restriction and high flexibility. The data format is BSON, which is a kind of jSON-like storage format, called Binary JSON. Like JSON, BSON supports embedded document objects and array objects.
Contrast this with the concept of a relational database
Mysql | MongoDB |
---|---|
Database (Database) | Database (Database) |
The Table (Table). | A Collection |
The Row (line) | Document |
The Column (Column) | Field = Field |
The data format
MongoDB stores the data as a document, in BSON format. Consists of keys and values.
{
"_id" : ObjectId("5e141148473cce6a9ef349c7"),
"title" : "Batch update"."url" : "http://cxytiandi.com/blog/detail/8"."author" : "yinjihuan"."tags" : [
"java"."mongodb"."spring"]."visit_count" : NumberLong(10),
"add_time" : ISODate("The 2019-02-11 T07: yea. 936 + 0000")}Copy the code
Usage scenarios
- Large data storage scenario
MongoDB has its own replica set and sharding, which is naturally suitable for a large number of scenarios. It is very convenient for developers to divide libraries and tables through middleware.
- Operation Log Storage
Most of the time, we need to store some operation logs, such as those of the last month, and the general practice is to clean them periodically. In MongoDB, there is a concept of fixed collection, we can specify the size when creating a collection, and the old data will be automatically removed when the data amount exceeds the size.
- Crawler data store
There are web pages and Json data, which are generally stored in the format of tables. If we use MongoDB, we can directly store the Json data captured in the collection without format restrictions.
- Social data storage
In social scenarios, MongoDB storage is used to store user address and location information, and the nearby people and locations are realized through geographical location index.
- E-commerce commodity storage
Different goods have different attributes. A common practice is to extract a common attribute list and associate it with the SPU. If you use MongoDB, you can directly embed attributes in the SPU.
narcissism
MongoDB has many functionality points, but for the most part we used only the simplest CRUD operations. The following solemn introduction of MongoDB function points, like you go dating, not a good introduction to their own advantages how can let you opposite the girl enchanted?
CRUD
CRUD is to add, delete, change and check, which is the most basic function of the database, the query also supports full text search, GEO geographical location query, etc.
- db.collection.insertOne()
A single document is inserted into the collection
- db.collection.insertMany()
Multiple documents are inserted into the collection
- db.collection.insert()
Single or multiple files are inserted into the collection
- db.collection.find( )
Query data
- db.inventory.updateOne()
Update a single
- db.inventory.updateMany()
Update more than
- db.inventory.deleteOne( )
Delete a single document
- db.inventory.deleteMany()
Deleting Multiple Documents
Aggregation
Aggregation operations are used for data statistics. For example, Mysql provides count, sum, group by, and other functions, while MongoDB provides Aggregation operations corresponding to Aggregation.
There are two ways to aggregate data, one is aggregate and the other is MapReduce.
The following figure shows how aggregate works:
Aggregation comes with a lot of built-in functions that allow us to calculate the data we want.
$project: Modifies the structure of the input document. It can be used to rename, add, or delete domains, create computed results, and nest documents.
Match uses MongoDB’s standard query operations.
$limit: Limits the number of documents returned by the MongoDB aggregation pipeline.
$skip: Skips a specified number of documents in the aggregation pipe and returns the remaining documents.
$group: Groups documents in a collection that can be used for statistical results.
$sort: Outputs by sorting the input documents.
$geoNear: Outputs an ordered document close to a geographic location.
$unwind: Unwinds an array type field in a document into multiple pieces, each containing a value from the array.
The following diagram shows how MapReduce works:
Query specifies the query condition. Only the data with status=A is processed.
In the MAP stage, the data are grouped and aggregated, that is, the effect of the third part is formed, and the re-statistics are removed according to CUST_ID.
The key in reduce is cust_id, and values is the amount set of the sum. The sum operation is then performed, and the final result is output to a set through out.
Transactions
MongoDB initially does not support transactions; in MongoDB, operations on individual documents are atomic operations. Therefore, the embedded documents and arrays can be used to describe the relationship between data during the redesign, which eliminates the need to operate across multiple documents and collections, thus eliminating the need for multi-document transactions in many practical use cases through single-document atomicity.
Everything is limited, some scenarios can not be completely nested to describe the relationship of data, or there will be multiple sets, for users of MongoDB, if it can support transactions is very convenient.
True to expectations, MongoDB version 4.0 has brought native transactions.
Indexes
I don’t need to say more about indexes. We all know what they do. Single index, composite index, full text index, Hash index, etc.
db.collection.createIndex({user_id: 1, add_time: 1}, {background: true})
Copy the code
Setting background to true will block other database operations while building the index. Background specifies how to create the index in the background. Default is false. This is a bloody lesson, remember remember.
Security
The security of MongoDB needs to be paid attention to. At present, we do not know whether there are mandatory restrictions on the startup. In the past, authentication mode can not be specified when the startup, that is, access without password, and then many people directly use the default port, which is exposed on the public network, giving opportunities to criminals and causing data deletion. Cases of needing bitcoin to retrieve data abound.
Still want to open security authentication, built in a lot of roles, different roles can operate the content is not the same, the control is more detailed.
Replication
A replica set is a set of MongoDB instances of the same dataset that stores data on multiple nodes simultaneously, improving availability. Master nodes are responsible for writing and slave nodes for reading, improving overall performance.
A replica set consists of the following components:
Primary: The Primary node receives all write operations.
Secondaries: The secondary node replicates data from the primary node and maintains the same data as the primary node. Used for query operations.
Arbiter: Arbiter nodes do not store data and only participate in elections.
Sharding
Sharding is the absolute highlight of MongoDB, splitting data horizontally across multiple nodes. MongoDB sharding is automatic, we only need to configure sharding rules, it can automatically maintain data and store to different nodes. MongoDB uses sharding to support large data storage and high-throughput operations.
The following figure shows the architecture of Mongodb sharding cluster:
MongoDB sharding cluster consists of the following components:
Shard: Data of each Shard is an independent and complete copy. And can be deployed as a replica set.
Mongos: Mongos is a query router, a layer between the client and server. Requests are routed directly to Mongos and routed to specific shards.
Config Servers: stores routing information about all nodes and fragmented data in the cluster.
GridFS
GridFS is a submodule of MongoDB and is used to store files in MongoDB, which is equivalent to a distributed file system built into MongoDB.
In essence, files are stored in chunks in collections. The default file collections are fs.files and fs.chunks.
Fs. files is the basic information of stored files, such as file name, size, upload time, MD5, etc. Fs. chunks is where the real data of a file is stored. A file is divided into chunks of 256K each.
If your project uses MongoDB, you can use GridFS to build a file system without having to buy third-party storage.
The advantage of GridFS is that you don’t need to build a separate file system. You can use Mongodb directly. Backup and sharding rely on Mongodb, which is easy to maintain.
Knowledge summary
Below is my summary of the knowledge, as a backend development, grasp the following content is good, after all, we’re not going to rob the DBA job, if you can spare time to learn in accordance with the following points for study, a few years ago I recorded a video, on my website, most of the content covered.
Work with
Compare the syntax of MongoDB and Mysql
Spring Boot integrates MongoDB
Add MongoDB dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
Copy the code
Configure the MongoDB information:
spring.data.mongodb.database=testHost =localhost spring.data.mongodb.port=27017 // The user name and password are omitted.......Copy the code
MongoTemplate can be injected directly to MongoDB:
@Autowired
private MongoTemplate mongoTemplate;
Copy the code
Used in the column
Create an entity class that corresponds to the MongoDB collection
@Data
@Document(collection = "article_info")
public class Article {
@Id
@GeneratedValue
private Long id;
@Field("title")
private String title;
@Field("url")
private String url;
@Field("author")
private String author;
@Field("tags")
private List<String> tags;
@Field("visit_count")
private Long visitCount;
@Field("add_time")
private Date addTime;
}
Copy the code
The final format stored in the data is as follows:
{
"_id" : ObjectId("5e141148473cce6a9ef349c7"),
"title" : "Batch update"."url" : "http://cxytiandi.com/blog/detail/8"."author" : "yinjihuan"."tags" : [
"java"."mongodb"."spring"]."visit_count" : NumberLong(10),
"add_time" : ISODate("The 2019-02-11 T07: yea. 936 + 0000")}Copy the code
Insert data
Article article = new Article();
article.setTitle(MongoTemplate Basic Use);
article.setAuthor("yinjihuan");
article.setUrl("http://cxytiandi.com/blog/detail/1");
article.setTags(Arrays.asList("java"."mongodb"."spring"));
article.setVisitCount(0L);
article.setAddTime(new Date());
mongoTemplate.save(article);
Copy the code
Database syntax
db.article_info.save({
"title": "Batch update"."url": "http://cxytiandi.com/blog/detail/8"."author": "yinjihuan"."tags": [
"java"."mongodb"."spring"]."visit_count": NumberLong(10),
"add_time": ISODate("The 2019-02-11 T07: yea. 936 + 0000")})Copy the code
Update the data
Query query = Query.query(Criteria.where("author").is("yinjihuan"));
Update update = Update.update("title"."MongoTemplate")
.set("visitCount", 10);
mongoTemplate.updateMulti(query, update, Article.class);
Copy the code
Database syntax
db.article_info.updateMany(
{"author":"yinjihuan"},
{"$set":
{
"title":"MongoTemplate"."visit_count": NumberLong(10)
}
}
)
Copy the code
Delete the data
Query query = Query.query(Criteria.where("author").is("yinjihuan"));
mongoTemplate.remove(query, Article.class);
Copy the code
Database syntax
db.article_info.remove({"author":"yinjihuan"})
Copy the code
Query data
Query query = Query.query(Criteria.where("author").is("yinjihuan"));
List<Article> articles = mongoTemplate.find(query, Article.class);
Copy the code
Database syntax
db.article_info.find({"author":"yinjihuan"})
Copy the code
Storing files
File file = new File("/Users/yinjihuan/Downloads/logo.png"); InputStream content = new FileInputStream(file); DBObject Metadata = new BasicDBObject("userId"."1001");
ObjectId fileId = gridFsTemplate.store(content, file.getName(), "image/png", metadata);
Copy the code
Source reference
Github.com/yinjihuan/s…
Client Recommendation
Download address:
studio3t.com/download/
spring-boot-starter-mongodb-pool
Finally, I recommend a small framework written by myself: enhanced Mongodb configuration in Spring Boot, multi-data sources, connection pooling
Github.com/yinjihuan/s…
If you are interested, you can pay attention to my wechat public number, Simtiandi, and read more technical articles for the first time. I also have some open source code on GitHub github.com/yinjihuan