Python3操作MongoDB数据库
0. 写在前面
- Linux:
Ubuntu Kylin 16.04
- MongoDB:
MongoDB3.2.7
- Python:
Anaconda With Python3.7
1. 安装开源驱动库pymongo
pymongo驱动程序可以直接连接MongoDB数据库
zhangsan@node01:~$ conda create -n py39 python = 3.9
zhangsan@node01:~$ conda activate py39
(py39) zhangsan@node01:~$ python3 -m pip install pymongo
Collecting pymongo
Downloading pymongo-4.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (479 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 479.2/479.2 kB 37.7 kB/s eta 0:00:00
Installing collected packages: pymongo
Successfully installed pymongo-4.2.0
(py39) zhangsan@node01:~$ vim pyinsert.py
from pymongo import MongoClient
from random import randint
import datetime
client = MongoClient('localhost',27017)
db = client.taobao
order = db.order_info
status = ['A','B','C']
cust_id = ['A123','B123','C123']
price = [500,200,250,300]
sku = ['mmm','nnn']
for i in range(1,100):
items = []
item_count =randint(2,6)
for n in range(item_count):
items.append({"sku":sku[randint(0,1)],"qty":randint(1,10),"price":randint(0,5)})
new = { "status":status[randint(0,2)], "cust_id":cust_id[randint(0,2)], "price":price[randint(0,3)], "ord_date":datetime.datetime.utcnow(), "items":items }
print(new)
order.insert_one(new)
print(order.estimated_document_count())
- 启动MongoDB
(py39) zhangsan@node01:/usr/local/mongodb-3.2.7$ mongod --config /mongodb/single/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3493
child process started successfully, parent exiting
- 查看服务
(py39) zhangsan@node01:/usr/local/mongodb-3.2.7$ ps -ef | grep mongo
zhangsan 3493 1 3 00:56 ? 00:00:01 mongod --config /mongodb/single/mongod.conf
zhangsan 3511 3214 0 00:57 pts/0 00:00:00 grep --color=auto mongo
- 执行py程序
(py39) zhangsan@node01:~$ python3 pyinsert.py
pymongo.errors.ConfigurationError: Server at localhost:27017 reports wire version 4, but this version of PyMongo requires at least 6 (MongoDB 3.6).
很明显,此处需要降低驱动pymongo的版本
- 卸载pymongo
(py39) zhangsan@node01:~$ pip uninstall -y pymongo
Found existing installation: pymongo 4.2.0
Uninstalling pymongo-4.2.0:
Successfully uninstalled pymongo-4.2.0
(py39) zhangsan@node01:~$ pip3 list
Package Version
------------ ------------
certifi 2022.6.15
pip 22.1.2
setuptools 63.4.1
- 安装pymongo3.6版本
(py39) zhangsan@node01:~$ pip3 install pymongo==3.6
Collecting pymongo==3.6
Downloading pymongo-3.6.0.tar.gz (583 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 583.3/583.3 kB 52.1 kB/s eta 0:00:00
Preparing metadata (setup.py) ... done
Building wheels for collected packages: pymongo
Building wheel for pymongo (setup.py) ... done
Created wheel for pymongo: filename=pymongo-3.6.0-cp39-cp39-linux_x86_64.whl size=272327 sha256=72a9dacdef7afa26cb169debe429964d1b5ab119ee39e7ec4c3eb5c59c5433c0
Stored in directory: /home/zhangsan/.cache/pip/wheels/6c/97/2e/74c0fd06fe0df24ded743a8af0707d1b5d618a0e8d321ba85c
Successfully built pymongo
Installing collected packages: pymongo
Successfully installed pymongo-3.6.0
(py39) zhangsan@node01:~$ pip3 list | grep pymongo
pymongo 3.6.0
- 继续报错:
AttributeError: module 'platform' has no attribute 'linux_distribution'
(py39) zhangsan@node01:~$ python3 pyinsert.py
Traceback (most recent call last):
File "/home/zhangsan/pyinsert.py", line 1, in <module>
from pymongo import MongoClient
File "/usr/local/anaconda/envs/py39/lib/python3.9/site-packages/pymongo/__init__.py", line 87, in <module>
from pymongo.mongo_client import MongoClient
File "/usr/local/anaconda/envs/py39/lib/python3.9/site-packages/pymongo/mongo_client.py", line 52, in <module>
from pymongo.client_options import ClientOptions
File "/usr/local/anaconda/envs/py39/lib/python3.9/site-packages/pymongo/client_options.py", line 23, in <module>
from pymongo.pool import PoolOptions
File "/usr/local/anaconda/envs/py39/lib/python3.9/site-packages/pymongo/pool.py", line 124, in <module>
platform.linux_distribution() if part])),
AttributeError: module 'platform' has no attribute 'linux_distribution'
关于这个error,其实是Python自身的原因
Python从3.8版本开始,已经从平台模块中删除了“linux_distribution()”函数
前文使用的是Python3.9,解决这个问题有两种方法:
- 修改Python3.9源码
- 降低Python版本到3.7
- 新建一个Python3.7虚拟环境
zhangsan@node01:~$ conda create -n pymongo python = 3.7
zhangsan@node01:~$ conda activate pymongo
(pymongo) zhangsan@node01:~$ python3 -m pip install pymongo=3.6
Collecting pymongo
Downloading pymongo-3.6.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (479 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 479.2/479.2 kB 37.7 kB/s eta 0:00:00
Installing collected packages: pymongo
Successfully installed pymongo-3.6.0
- 再次执行py程序,运行成功
(pymongo) zhangsan@node01:~$ python3 pyinsert.py
上述代码中使用了estimated_document_count()
函数,该函数是MongoDB4.0.3的新功能,此处会出现错误,改为count()函数即可
2. 参考
https://stackoverflow.com/questions/63636697/error-module-platform-has-no-attribute-linux-distribution