Python Toolbox Series (29)

MongoDB is known as a document database and is currently a rapidly developing NoSQL database. It is written in C++ language and aims to provide scalable high-performance data storage solutions for WEB applications. MongoDB is the most feature-rich among non-relational databases, and is most similar to relational databases. The data structure it supports is very loose, which is a bson format similar to json, so it can store more complex data types. The biggest feature of MongoDB is that the query language it supports is very powerful. Its syntax is somewhat similar to object-oriented query language. It can almost realize most of the functions similar to single-table query of relational database, and it also supports indexing of data.

The main features are:

◆Set-oriented storage, easy to store data of object type.

◆Mode free.

◆Support dynamic query.

◆Support full index, including internal objects.

◆Support query.

◆Support replication and failure recovery.

◆ use efficient binary data storage, including large objects (such as video, etc.).

◆Automatically handle fragments to support the scalability of the cloud computing level.

◆Support Golang, RUBY, PYTHON, JAVA, C++, PHP, C# and other languages.

◆The file storage format is BSON (an extension of JSON).

◆It can be accessed through the network.

Its main scenarios are as follows:

◆Real-time data processing. It is well suited for real-time inserts, updates and queries, with the replication and high scalability required for real-time data storage.

◆Cache. Due to its high performance, it is suitable as a caching layer for information infrastructure. After the system is restarted, the persistent cache layer built by it can prevent the underlying data source from being overloaded.

◆High scalability scene. Ideal for databases consisting of tens or hundreds of servers, it already includes built-in support for the MapReduce engine.

The scenarios that do not apply are as follows:

◆Requires a highly transactional system.

◆Traditional business intelligence applications.

◆ complex cross-document (table) cascade query.

The MongoDB server can run on Linux, Windows or mac os x platforms, supports 32-bit and 64-bit applications, and the default port is 27017. It is recommended to run on 64-bit non-Windows platforms. One of the author's favorite features is the flexible sharding mechanism of MongoDB. The configured sharding mechanism can not only achieve the goal of load balancing, but also enable redundant backup.

1. Installation under Ubuntu bionic

Just use the deb file provided by the official website to install it.

# server
wget https://repo.mongodb.org/apt/ubuntu/dists/focal/mongodb-org/4.4/multiverse/binary-arm64/mongodb-org-server_4.4.15_arm64.deb
# mongos,分片服务器
wget https://repo.mongodb.org/apt/ubuntu/dists/bionic/mongodb-org/4.4/multiverse/binary-amd64/mongodb-org-mongos_4.4.15_amd64.deb

# setup server
dpkg -i mongodb-org-server_5.0.9_amd64.deb 

# 检查相关状态
systemctl status mongod

# 自动启动
systemctl enable mongod

# 使用命令行操作mongodb
mongo

Modify the configuration file /etc/mongod.conf to obtain the ability of remote access.

cat /etc/mongod.conf |grep bind
sed -i 's/127.0.0.1/0.0.0.0/g' /etc/mongod.conf
cat /etc/mongod.conf |grep bind
systemctl restart mongod
systemctl status mongod

MongoDB provides a wealth of commands, experts can fully control the database and data, but it is not very friendly for beginners. For this reason, it is recommended to use MongoDBCompass officially provided by MongoDB, just download and install it on Windows, and then connect to the remote MongoDB database. The effect is shown in the following figure:

 It can be seen from the figure that the server is remote, and through this GUI interface, commands can also be entered to operate the MongoDB database.

Two, Python connection using MongoDB

It is very convenient to operate MongoDB with Python. You can directly insert data without defining the table structure. Using the pymongo module, you can realize the interaction between MongoDB and Python. The installation command is as follows:

pip install pymongo

The following code illustrates the process of creating/using a custom database, inserting three records, and querying.

import pymongo

client = pymongo.MongoClient("mongodb://192.168.0.66:27017")
print(client.list_database_names())
db = client['raindrop']
collection = db['pm25']

# 增加一条
stu1 = {'id': '001', 'area': 'office', 'pm25': 8}
result = collection.insert_one(stu1)

# 增加多条
stu2 = {'id': '002', 'area': 'field01', 'pm25': 15, "temp": 10.5}
stu3 = {'id': '003', 'monitor': 'wangwu', 'area': 'field02', 'age': 20}
result = collection.insert_many([stu2, stu3])

# 查询记录
ret = collection.find_one({'area': 'office'})
print(ret)

Through MongoDB Compass, you can see that the database saves three documents.

 Due to the modeless feature of MongoDB, it is very convenient to use, especially for semi-structured data storage.

Guess you like

Origin blog.csdn.net/shaanxihualu/article/details/129796611
29