1. Introduction to MongoDB
1.1 What is MongoDB
MongoDB is a database based on distributed file storage. Written by C++ language. Designed to provide scalable high-performance data storage solutions for WEB applications. MongoDB is a product between relational and non-relational databases. It is the most versatile and most like relational database among non-relational databases. The data structure it supports is very loose, it is a bson format similar to json, so it can store more complex data types.
1.2 MongoDB features
The biggest feature of Mongo 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 achieve most of the functions similar to single-table queries in relational databases, and it also supports indexing of data.
It is characterized by high performance, easy deployment, easy use, and very convenient data storage. The main features are:
- For collection storage, it is easy to store object type data.
- Model freedom.
- Support dynamic query.
- Supports full indexing, including internal objects.
- Support query.
- Supports replication and failure recovery.
- Use efficient binary data storage, including large objects (such as videos, etc.).
- Automatically handle fragments to support the scalability of cloud computing.
- Support RUBY, PYTHON, JAVA, C++, PHP, C# and other languages.
- The file storage format is BSON (an extension of JSON).
1.3 Usage scenarios
- Big amount of data
- Frequent write operations
- Low value
1. Application server logging
Everyday we will store some application logs in text format files, which is not convenient for viewing and statistics. Through MongoDB storage, it can not only store and count well, but also facilitate the inconsistency of log data formats in different business scenarios .
2. Crawling and storage of third-party information
We will inevitably use third-party data in some business scenarios. When accessing multiple third-party platforms, we need to consider the inconsistency of the data format of each platform and the design of its own storage system structure . At this time, we use MongoDB for storage to avoid this problem.
3. Operation and maintenance monitoring system
In some large-scale projects, monitoring is essential. The content to be monitored by the monitoring system may be changeable at any time. At this time, using MongoDB is very convenient. There is no need to modify the structure of the database , just adjust it flexibly according to business needs. Greatly reduce development costs .
4. O2O business scenario
Store the information (including location information) of the express rider and express merchant in MongoDB, and then query it through MongoDB's geographic location , so that it is very convenient to find nearby merchants, riders and other functions.
5. Game business scenarios
Use MongoDB to store game user information, and the user's equipment, points, etc. are directly stored in the form of embedded documents, which is convenient for query and update.
6. Social business scenarios
Use MongoDB to store and store user information, as well as user circle information published by users, and realize functions such as nearby people and places through geographic indexing.
7. Internet of Things business scenarios
Use MongoDB to store all connected smart device information and log information reported by the device, and perform multi-dimensional analysis of this information.
8. Video live broadcast business scenario
Live video, use MongoDB to store user information, gift information, etc.
1.4 MongoDB architecture
The logical structure of MongoDB is a hierarchical structure. It is mainly composed of three parts: document, collection, and database. The logical structure is user-oriented, and what users use to develop applications using MongoDB is the logical structure.
- MongoDB document (document) is equivalent to a row of records in a relational database.
- Multiple documents form a collection, which is equivalent to a table in a relational database.
- Multiple collections, logically organized together, are the database.
- A MongoDB instance supports multiple databases.
The hierarchical structure of document, collection, and database is as follows:
MongoDb | Relational database Mysql |
---|---|
Databases | Databases |
Collections | Table |
Document | Row |
1.5 MongoDB data types
type of data | description |
---|---|
String | String. Data types commonly used to store data. In MongoDB, UTF-8 encoded strings are legal. |
Integer | Integer value. Used to store values. According to the server you use, it can be classified as 32-bit or 64-bit. |
Boolean | Boolean value. Used to store boolean values (true/false). |
Double | Double-precision floating-point value. Used to store floating point values. |
Array | Used to store an array or list or multiple values as a key. |
Timestamp | Timestamp. Record the specific time when the document was modified or added. |
Object | Used for embedded documents. |
Null | Used to create null values. |
Date | Date time. Use UNIX time format to store the current date or time. You can specify your own date and time: create a Date object and pass in the year, month, and day information. |
Object ID | Object ID. The ID used to create the document. |
Binary Data | Binary data. Used to store binary data. |
Code | Type of code. Used to store JavaScript code in the document. |
Regular expression | Regular expression type. Used to store regular expressions. |
Special Instructions:
-
ObjectIdObjectId is similar to a unique primary key and can be quickly generated and sorted. It contains 12 bytes, which means:
- The first 4 bytes represent the creation of the unix timestamp, UTC time in Greenwich Mean Time, which is 8 hours later than Beijing time
- The next 3 bytes are the machine identification code
- The next two bytes consist of the PID of the process id
- The last three bytes are random numbers
-
Documents stored in MongoDB must have an _id key. The value of this key can be of any type, and the default is an ObjectId object
-
Timestamp BSON has a special timestamp type, which is not related to ordinary date types. The timestamp value is a 64-bit value. among them:
- The first 32 bits is a time_t value [the number of seconds from the Unix era (January 1, 1970)]
- The last 32 bits is an increasing ordinal number operated in a certain second
-
In a single mongod instance, the timestamp value is usually unique.
-
Date
Represents the current number of milliseconds from the Unix era (January 1, 1970). The date type is signed, and negative numbers indicate dates before 1970.