MongoDB document--architecture system

Adam:

        At the beginning of learning, first understand the architecture system of the target knowledge. Can get twice the result with half the effort.

 Portal:

MongoDB Documentation--Basic Concepts - A Single Cheng Blog - CSDN Blog

MongoDB documentation--basic installation-linux installation (mongodb environment construction)-docker installation (mounting data volumes)-and detailed version comparison_一单成博客-CSDN Blog

 MongoDB Documentation - Basic Use - Using MongoDB Basic Statements in the Client (dos Window)/Visualization Tool Use regular expressions to find in

MongoDB document-advanced use-spring-boot integration using MongoDB --- MongoRepository to complete additions, deletions, changes and queries_One Single Cheng Blog-CSDN Blog 

MongoDB document-advanced use-spring-boot integration using MongoDB---MongoTemplate to complete additions, deletions, modifications and queries_One Single Cheng's Blog-CSDN Blog 

Architecture system

   MongoDB's architecture system consists of the following parts:

  1. Storage structure: MongoDB adopts a document-type storage structure. A database contains multiple collections, and a collection contains multiple documents.
  2. Storage form: MongoDB adopts binary storage form, and data is stored in the database in binary form.
  3. Directory structure: MongoDB service (mongod) is responsible for data storage and query, data routing service (mongos) is responsible for data and query routing, shell client (mongo) is used to interact with the database, import and export tool (mongoimport / mongoexport) is used for Import and export data, backup and recovery tools (mongodump / mongorestore) are used to back up and restore data, tools for pulling and replaying oplogs (mongooplog) are used to pull oplogs and replay data, and monitoring tools (mongostat, mongotop, mongosniff) are used for Monitor the running status of the database. The GridFS command line operation tool (mongofiles) is used to operate the GridFS file system. The performance test tool (mongoperf, which can only measure I/O for the time being) is used to test the performance of the database. The tool for viewing bson files (bsondump ) to view the content of the bson file.
  4. Data storage: MongoDB data is stored in a default data directory, which is located under data/db in the installation directory. Each database has a corresponding .ns file and some data files, and the data files will become more and more as the amount of data increases.
  5. Sharded cluster: MongoDB supports sharded cluster deployment, which can distribute data on multiple nodes to improve system scalability and reliability. In a sharded cluster, each node can store data and route queries, and each node has its own oplog and config set.

To sum up, MongoDB's architecture includes storage structure, storage form, directory structure, data storage, and fragmented clusters. This architecture system makes MongoDB have the advantages of high performance, high scalability, rich query language, easy to use, etc., and can meet the needs of different application scenarios.

Data storage architecture system in database

        The data structure of MongoDB is similar to that of a document database, and its data is stored in containers called "Collections". Each collection contains multiple documents (Documents), and each document is composed of multiple key-value pairs (Key-Value Pairs). These key-value pairs contain specific attributes and values, similar to fields (Fields) and data in relational databases.

In MongoDB, a database contains multiple collections, which can be organized and categorized in different ways. Similar to relational databases, MongoDB also supports the use of schemas (Schema) to define the structure and properties of documents.

Here is an example of a simple MongoDB data structure:

{
  "_id": ObjectId("5a5e0f551df5c11b5e5b56c3"),
  "name": "John Doe",
  "age": 30,
  "email": "[email protected]",
  "address": {
    "street": "123 Main St",
    "city": "Anytown",
    "state": "CA",
    "zip": "12345"
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "555-555-1234"
    },
    {
      "type": "work",
      "number": "555-555-5678"
    }
  ]
}

In this example, we have a collection called "users" that contains a document with attributes named "name", "age", "email", "address", and "phoneNumbers". Among them, "address" and "phoneNumbers" are nested objects and arrays that can contain more complex data types.

In short, MongoDB's data structure is based on documents, and each document contains a set of key-value pairs, where each key uniquely identifies an attribute, and each value is a data type. Unlike relational databases, data types in MongoDB can be complex objects and arrays, not just simple tables and fields.

Interpretation of structure

In MongoDB, a collection (Collection) is like a container for storing multiple documents (Documents). Each document is like a JSON object, containing a set of key-value pairs, where each key uniquely identifies a property and each value is a data type. Therefore, a collection can contain multiple documents, just like a table can contain multiple rows of data.

However, documents in MongoDB are not stored in collections like rows in a relational database. Instead, documents exist independently of each other, they are simply stored in collections so that they can be easily queried and managed. This also means that in MongoDB, the order of documents is indeterminate, they can be arranged and rearranged arbitrarily in the collection.

In summary, collections in MongoDB are similar to tables in relational databases, but they can contain multiple documents, each of which is an independent JSON object with its own key-value pairs and data types.

Similar to a collection is a table, which can contain many objects without specified fields.

Compared with mysql, the objects in the tables in mysql have already specified fields and formats, which is the advantage of MongoDB. More flexible, they only need to be arranged in a collection to be stored and can be queried and used.

Working principle of mongdb-add/delete/modify/check principle

MongoDB is a document-based NoSQL database that works as follows:

Add data:

When you need to add data to MongoDB, you can insert one or more documents into the collection through insertOne()or and other methods. insertMany()These documents can be in JSON format, or other data types, such as binary data or geographic location information.

delete data:

To delete data in MongoDB, you can use methods such as deleteOne(), deleteMany()or , findOne()to delete eligible documents. Deleting permanently removes the selected documents from the collection without affecting other documents in the collection.

update data:

To update data in MongoDB, you can use methods such as updateOne(), updateMany()or findOneAndUpdate(), to modify eligible documents. An update operation can modify certain properties in a document or add new properties, or it can replace an entire document with another document.

Query data:

To query the data in MongoDB, you can use find(), findOne(), count(), aggregate()and other methods to find documents that meet the conditions. Query operations can perform operations such as filtering, sorting, and paging based on the attributes in the document, and can also use aggregation functions to perform statistics and analysis on the data.

In short, the working principle of MongoDB is to manage and operate document collections through operations such as adding, deleting, modifying, and checking. These operations can be performed through MongoDB's driver or client, supporting multiple programming languages ​​and platforms. Since MongoDB is a NoSQL database, it has a flexible data structure and query language that can adapt to applications of different types and sizes.

How mongdb works - advanced usage indexing principles

MongoDB's index is a data structure used to improve query efficiency and speed up query functions. Indexes are created on documents in a collection, which can contain one or more fields, and are sorted in a specific order.

Indexing works as follows:

Create an index:

Creating an index on a collection requires the use of createIndex()methods that specify the fields and ordering of the index. ageFor example, the following code will create an ascending index on a field in a collection :

db.collection.createIndex({ age: 1 })

Query index: 

After an index is created, it can find()be queried using methods. MongoDB will query according to the sorting method of the index, so as to quickly locate the required data. For example, the following code will query for all documents with an age greater than or equal to 18:

db.collection.find({ age: { $gte: 18 } })

Update index: 

When the data in the collection changes, the index needs to be updated accordingly. When inserting, updating, or deleting documents, MongoDB automatically updates the related indexes. If you want to update the index manually, you can use update()method or save()method to update the document.

Delete the index:

To delete an index in a collection, you can use dropIndex()the method. For example, the following code will drop agean index created on a field:

db.collection.dropIndex({ age: 1 })

In short, MongoDB's index is a data structure used to improve query efficiency and speed up query functions. It can be created on documents in a collection, contains one or more fields, and is sorted in a specific order. The creation, query, update, and deletion of indexes all need to use the corresponding MongoDB methods for operations.

Guess you like

Origin blog.csdn.net/weixin_72186894/article/details/132042351